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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:15:34  来源:igfitidea点击:

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

vbams-wordword-vba

提问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- InlineShapesand Shapes. It's quite easy to check name of shape object with some VBA code:

MS Word 中有两种类型的形状 -InlineShapesShapes. 使用一些 VBA 代码检查形状对象的名称非常容易:

  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.
  1. 选择形状
  2. 按 Alt+F11 打开 VBA 编辑器
  3. 在立即窗口中执行此代码: ? Selection.ShapeRange.Name
  4. 结果你得到了形状的名称。

InlineShapedoesn't have name property therefore you can't check it's name until you promote your InlineShapeto Shapetype object.

InlineShape没有 name 属性,因此您无法检查它的名称,直到您提升您InlineShapeShape类型对象。

回答by Bruno Bieri

Microsoft Word 2010onwards

Microsoft Word 2010向前

From Microsoft Word 2010onwards (2010, 2013and 2016) there is an "Selection Pane" included in Microsoft Word. On the selection pane the Microsoft Word InlineShapesas well as the Shapesare listed and named.

Microsoft Word 2010( 2010,20132016)开始, 中包含一个“选择窗格” Microsoft Word。在选择窗格中,Microsoft WordInlineShapes以及Shapes被列出并命名。

You can find the Selection Panein the menu under

您可以Selection Pane在菜单下找到

  1. "Home"-tab
  2. "Editing"-group
  3. "Select"-button
  4. "Selection Pane..."
  1. “主页”-选项卡
  2. “编辑”组
  3. “选择”-按钮
  4. “选择面板……”


Microsoft Wordversions before 2010

Microsoft Word2010 年之前的版本

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