Word VBA Text box ID

Hi Experts,

Please could you help, I have a shape in a Word 2010 template and I need to find the ID of the text box in VBA.

Thanks,

Paul
MrDavidThornAsked:
Who is Participating?
 
GrahamSkanConnect With a Mentor RetiredCommented:
This walks through all the headers (up to three) in all the sections in the active document
Sub HeaderShapesIDByText()
    Dim sh As Shape
    Dim sec As Section
    Dim hdr As HeaderFooter
    
    For Each sec In ActiveDocument.Sections
        For Each hdr In sec.Headers
            For Each sh In hdr.Shapes
                If sh.TextFrame.HasText Then
                    If sh.TextFrame.TextRange.Text = "My Shape" Then
                        Debug.Print sh.ID
                    End If
                End If
            Next sh
        Next hdr
    Next sec
End Sub

Open in new window

0
 
GrahamSkanRetiredCommented:
How do you want to define the particular shape? You can list all the shape IDs in a document:
Sub ShapesID()
    Dim sh As Shape
    For Each sh In ActiveDocument.Shapes
        Debug.Print sh.ID
    Next sh
End Sub

Open in new window

0
 
MrDavidThornAuthor Commented:
Hi,

I have some text in shape and I'm trying to return the ID of that shape based on the specific text.

Thanks
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
GrahamSkanRetiredCommented:
You will need something like this:
Sub ShapeIDByText()
    Dim sh As Shape
    For Each sh In ActiveDocument.Shapes
        If sh.TextFrame.HasText Then
            If sh.TextFrame.TextRange.Text = "My Shape" Then
                Debug.Print sh.ID
            End If
        End If
    Next sh
End Sub

Open in new window

0
 
MrDavidThornAuthor Commented:
Thanks again. This works nicely in the active document, but would you know how I can loop through to find textboxes in the header, it appears they don't get included.

0
 
MrDavidThornAuthor Commented:
Thanks very much, that works a treat.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.