[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 323
  • Last Modified:

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
0
MrDavidThorn
Asked:
MrDavidThorn
  • 3
  • 3
1 Solution
 
GrahamSkanCommented:
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
 
GrahamSkanCommented:
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
GrahamSkanCommented:
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
 
MrDavidThornAuthor Commented:
Thanks very much, that works a treat.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now