How do I retrieve the name of a shape in MS Word?

只谈情不闲聊 提交于 2019-12-04 07:38:06

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:

  1. select shape
  2. press Alt+F11 to open VBA Editor
  3. in Immediate window execute this code: ? Selection.ShapeRange.Name
  4. 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.

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

  1. "Home"-tab
  2. "Editing"-group
  3. "Select"-button
  4. "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

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

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!