如何在MS Word中检索形状的名称?

时间:2013-07-16 15:06:59

标签: vba ms-word word-vba

在Excel中,我们在左上角有“名称框”,但我找不到在Word中检索形状名称的方法。我该怎么做?

5 个答案:

答案 0 :(得分:8)

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

  1. 选择形状
  2. 按Alt + F11打开VBA编辑器
  3. 在立即窗口中执行以下代码:? Selection.ShapeRange.Name
  4. 因此您获得了形状的名称。
  5. InlineShape没有名称属性,因此在将InlineShape提升为Shape类型对象之前,您无法检查其名称。

答案 1 :(得分:6)

Microsoft Word 2010以后

Microsoft Word 2010开始(201020132016),有一个"选择窗格"包含在Microsoft Word中。 在选择窗格中,列出并命名了Microsoft Word InlineShapes以及Shapes

您可以在

下的菜单中找到Selection Pane
  1. "首页" -Tab
  2. "编辑" - 基团
  3. "选择" - 按钮
  4. "选择窗格......"
  5. 较早Microsoft Word个版本

    对于旧的Microsoft Word(20032007)版本使用VBA方法,因为Kazimierz Jawor发布了此问题的另一个答案:https://stackoverflow.com/a/17680650/1306012

答案 2 :(得分:0)

最方便的方法是创建一个宏按钮,可以从标签中访问(例如,Home,Insert等)。这样,您只需单击形状,单击宏按钮,然后瞧 - 形状名称将显示在消息框中(弹出窗口)。

使用以下代码:

MsgBox ActiveWindow.Selection.ShapeRange(1).name

答案 3 :(得分:0)

正确答案,我希望)))

    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

答案 4 :(得分:0)

Word 2007 适用于图片,其余未测试

Sub S___FindShapetypeOfSelectedShape()
    
    '1======= msgbox if floating shape selected
        On Error GoTo NOT_FLOATING_SHAPE 'go to check for inline shape
            MsgBox "Floating shape, " & ActiveWindow.Selection.ShapeRange(1).Name '"Picture 1480"; blue dottedlines= "picture 4"
            Exit Sub
    
    NOT_FLOATING_SHAPE:
        'on error goto 0 'use for testing
        On Error GoTo NO_SHAPE_FOUND 'doesnt work???
    
    '2.=========
            MsgBox "Inline Shape type NUMBER = " & ActiveWindow.Selection.InlineShapes(1).Type '
    
    '2a_________check for each type of inline shape
        '!!!(to see if msgbox draft below can be fixed)
        If ActiveWindow.Selection.InlineShapes(1).Type = wdNoSelection Then
            MsgBox "No selection"
            Exit Sub
    
        ElseIf ActiveWindow.Selection.InlineShapes(1).Type = wdInlineShapeChart Then
            MsgBox "wdInlineShapeChart"
            Exit Sub
    
        ElseIf ActiveWindow.Selection.InlineShapes(1).Type = wdInlineShapeDiagram Then
            MsgBox "wdInlineShapeDiagram"
            Exit Sub
        
        ElseIf ActiveWindow.Selection.InlineShapes(1).Type = wdInlineShapeEmbeddedOLEObject Then
            MsgBox "wdInlineShapeEmbeddedOLEObject"
            Exit Sub
    
        ElseIf ActiveWindow.Selection.InlineShapes(1).Type = wdInlineShapeHorizontalLine Then
            MsgBox "wdInlineShapeHorizontalLine"
            Exit Sub
    
        ElseIf ActiveWindow.Selection.InlineShapes(1).Type = wdInlineShapeLinkedOLEObject Then
            MsgBox "wdInlineShapeLinkedOLEObject"
            Exit Sub
    
        ElseIf ActiveWindow.Selection.InlineShapes(1).Type = wdInlineShapeLinkedPicture Then 'EMPTY FRAMES?
            MsgBox "wdInlineShapeLinkedPicture"
            Exit Sub
    
        ElseIf ActiveWindow.Selection.InlineShapes(1).Type = wdInlineShapeLinkedPictureHorizontalLine Then
            MsgBox "wdInlineShapeLinkedPictureHorizontalLine"
            Exit Sub
    
        ElseIf ActiveWindow.Selection.InlineShapes(1).Type = wdInlineShapeLockedCanvas Then
            MsgBox "wdInlineShapeLockedCanvas"
            Exit Sub
    
        ElseIf ActiveWindow.Selection.InlineShapes(1).Type = wdInlineShapeOLEControlObject Then
            MsgBox "wdInlineShapeOLEControlObject"
            Exit Sub
    
        ElseIf ActiveWindow.Selection.InlineShapes(1).Type = wdInlineShapeOWSAnchor Then
            MsgBox "wdInlineShapeOWSAnchor"
            Exit Sub
    
        ElseIf ActiveWindow.Selection.InlineShapes(1).Type = wdInlineShapePicture Then 'DOESNT FIND SOME PICTURES PASTED FROM WEB!
            MsgBox "wdInlineShapePicture"
            Exit Sub
    
        ElseIf ActiveWindow.Selection.InlineShapes(1).Type = wdInlineShapePictureBullet Then
            MsgBox "wdInlineShapePictureBullet"
            Exit Sub
    
        ElseIf ActiveWindow.Selection.InlineShapes(1).Type = wdInlineShapePictureHorizontalLine Then
            MsgBox "wdInlineShapePictureHorizontalLine"
            Exit Sub
    
        ElseIf ActiveWindow.Selection.InlineShapes(1).Type = wdInlineShapeScriptAnchor Then
            MsgBox "wdInlineShapeScriptAnchor"
            Exit Sub
    
        ElseIf ActiveWindow.Selection.InlineShapes(1).Type = wdInlineShapeSmartArt Then
            MsgBox "wdInlineShapeSmartArt"
            Exit Sub
    End If
    
    NO_SHAPE_FOUND:
        MsgBox "No floating or inline shape selected!"
        
    End Sub