问题
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?
回答1:
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:
- 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.
InlineShape doesn't have name property therefore you can't check it's name until you promote your InlineShape to Shape type object.
回答2:
Microsoft Word 2010 onwards
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.
You can find the Selection Pane in the menu under
- "Home"-tab
- "Editing"-group
- "Select"-button
- "Selection Pane..."
older Microsoft Word versions
For older Microsoft Word (2003, 2007) versions use the VBA approach as Kazimierz Jawor posted as an other answer to this question: https://stackoverflow.com/a/17680650/1306012
回答3:
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
回答4:
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
来源:https://stackoverflow.com/questions/17680301/how-do-i-retrieve-the-name-of-a-shape-in-ms-word