vba 如何在 MS Word 中检索形状的名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17680301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I retrieve the name of a shape in MS Word?
提问by ftkg
In Excel we have the "Name Box" in the upper-left side, but I could not find a way to retrieve the name of a shape in Word. How do I do that?
在 Excel 中,我们在左上角有“名称框”,但我找不到在 Word 中检索形状名称的方法。我怎么做?
回答by Kazimierz Jawor
There are two types of shapes in MS Word- InlineShapes
and Shapes
. It's quite easy to check name of shape object with some VBA code:
MS Word 中有两种类型的形状 -InlineShapes
和Shapes
. 使用一些 VBA 代码检查形状对象的名称非常容易:
- select shape
- press Alt+F11 to open VBA Editor
- in Immediate window execute this code:
? Selection.ShapeRange.Name
- as a result you get name of the shape.
- 选择形状
- 按 Alt+F11 打开 VBA 编辑器
- 在立即窗口中执行此代码:
? Selection.ShapeRange.Name
- 结果你得到了形状的名称。
InlineShape
doesn't have name property therefore you can't check it's name until you promote your InlineShape
to Shape
type object.
InlineShape
没有 name 属性,因此您无法检查它的名称,直到您提升您InlineShape
的Shape
类型对象。
回答by Bruno Bieri
Microsoft Word 2010
onwards
Microsoft Word 2010
向前
From Microsoft Word 2010
onwards (2010
, 2013
and 2016
) there is an "Selection Pane" included in Microsoft Word
.
On the selection pane the Microsoft Word InlineShapes
as well as the Shapes
are listed and named.
从Microsoft Word 2010
( 2010
,2013
和2016
)开始, 中包含一个“选择窗格” Microsoft Word
。在选择窗格中,Microsoft WordInlineShapes
以及Shapes
被列出并命名。
You can find the Selection Pane
in the menu under
您可以Selection Pane
在菜单下找到
- "Home"-tab
- "Editing"-group
- "Select"-button
- "Selection Pane..."
- “主页”-选项卡
- “编辑”组
- “选择”-按钮
- “选择面板……”
Microsoft Word
versions before 2010
Microsoft Word
2010 年之前的版本
For older Microsoft Word (2003
, 2007
) versions use the VBA approach (?Selection.ShapeRange.Name
) as Kazimierz Jawor posted as an other answer to this question: https://stackoverflow.com/a/17680650
对于较旧的 Microsoft Word ( 2003
, 2007
) 版本,请使用 VBA 方法 ( ?Selection.ShapeRange.Name
) 作为 Kazimierz Jawor 发布的此问题的其他答案:https: //stackoverflow.com/a/17680650
回答by cinnamonandgravy
The most convenient method is to create a macro button, which is accessible from your tabs (e.g., Home, Insert, etc.). This way, you just click on the shape, click the macro button, and voila - the shape name displays in a message box (pop up window).
最方便的方法是创建一个宏按钮,可从您的选项卡(例如,主页、插入等)访问该按钮。这样,您只需单击形状,单击宏按钮,瞧 - 形状名称显示在消息框(弹出窗口)中。
Use the following code:
使用以下代码:
MsgBox ActiveWindow.Selection.ShapeRange(1).name
回答by Anton Mukhanin
Correct answer, I hope)))
正确答案,我希望)))
For Each ILShp In Doc.InlineShapes
If ILShp.Type = 5 Then ' 5 (wdInlineShapeOLEControlObject) - OLE control object. (ComboBox and CheckBox)
' if object is ComboBox
If CStr(ILShp.OLEFormat.ClassType) = "Forms.ComboBox.1" Then
Cb_Name = ILShp.OLEFormat.Object.Name ' retuns ComboBox1
endif
Next