如何检查哪些 shapes/objects 是 selected/active?

How to check which shapes/objects are selected/active?

我想检查哪些对象是 selected/active。

在 PowerPoint 和 Word 中这很容易,但在 Excel 中...我检查的任何东西都不起作用。 Globals.ThisAddin.ActiveWindow.Selection 是类型:动态。 我添加了对 VisualBasic 的引用,以访问 TypeName 函数。

如果选择了图表 returns 类型 "ChartObject"... 所以我将它设置为 ChartObject 类型的变量,但是我几乎无法访问它的任何属性和方法,例如,当我尝试读取该对象的名称或尝试从中获取 return 图表时,会出现错误。

When there is few shapes selected then TypeName function returns me type: "DrawingObjects"... but I am not able to read anything from it.我试图从中获取 ShapeRange,但又...错误。

你能告诉我如何获取所有选定的对象吗?

在Excel VBA TypeName() 中可以return 对应各种形状的大量名称中的任何一个。

一旦我们确定名称(由 TypeName() 定义)对应于特定的 Shape-type,我们就可以使用 NameSelection 定义特定形状:

Sub WhatIsSelected()
    Dim sh As Shape

    If TypeName(Selection) = "Rectangle" Then
        Set sh = ActiveSheet.Shapes(Selection.Name)
    End If

    MsgBox TypeName(Selection) & vbCrLf & sh.Name
End Sub

现在 sh 既是 Dimmed 又是 Set 我们可以获取它的所有属性并使用它的所有方法。

我设法创建了有效的代码(用于图表):

        public static List<XL.Chart> ReturnSelectedCharts(dynamic selection )
    {
        List<XL.Chart> charts=new List<XL.Chart>();

        XL.ShapeRange selectedShapeRange = null;
        XL.Chart chart=null;
        try
        {
            selectedShapeRange = Globals.ThisAddIn.Application.Selection.ShapeRange;
            for (int i = 1; i <= selectedShapeRange.Count; i++)
            {
                XL.Shape shape=selectedShapeRange.Item(i);
                if (shape.HasChart==MsoTriState.msoTrue)
                {
                    chart = shape.Chart;
                    charts.Add(chart);
                }

            }
        }
        catch
        {
        }
        if (charts.Count==0)
        {
            try
            {
                chart = Globals.ThisAddIn.Application.ActiveChart;
                charts.Add(chart);
            }
            catch (Exception)
            {
            }
        }

        return charts;
    }