如何在Excel VBA中获取选定的形状?

我插入了一个聪明的艺术,并将其转换为形状。 并通过点击select一个形状。

现在我想获得Shape形状的Shape对象。 我已经尝试过,但它会抛出exception。

dim shap as Excel.Shape = ExcelApp.Selection 

我可以通过迭代ActiveSheet.Shapes或像这样得到形状对象

 dim shap as Excel.Shape = ActiveSheet.Shapes.Item(1) 

但是,我怎么会知道这个形状被选中或不,真的需要帮助谢谢。

尝试Selection.ShapeRange以获取对形状对象的引用。

如果没有select任何或多个形状,则获得单个选定的形状或无。 显然,您可以放下MsgBox调用。

 Function GetSelectedShape() As Shape If TypeName(Selection) <> "Rectangle" Then MsgBox "Selection is not a single shape" Exit Function End If Dim oShapes As ShapeRange Set oShapes = Selection.ShapeRange If oShapes.Count <> 1 Then MsgBox "Selection is not a single shape" Exit Function End If Set GetSelectedShape = oShapes(1) End Function