none
Need help using VBA to get info from a Word 2007 ActiveX TEXTBOX RRS feed

  • Question

  • I'm trying to create a VBA script that allows me to search a number of ActiveX TEXTBOXES that are in a Word 2007 document. I was able to create a version that uses Activex TEXTBOXES that are "Inline with text" (right-click on the TextBox and select Format Control>Layout) with the following script....
    ----------------------------------------------------------------------
    For Each oCtl In Documents(NewFileName).InlineShapes
        If oCtl.OLEFormat.ProgID = "Forms.TextBox.1" Then
            Set oTB = oCtl.OLEFormat.Object
            MsgBox ("InlineShape>TextBox=" + oTB.Name)
        End If
    Next
    ----------------------------------------------------------------------
    ....but when I attempt to access info from an ActiveX TEXTBOXES that are "Infront of Text" using the script....
    ----------------------------------------------------------------------
    For Each oCtl In Documents(NewFileName).Shapes
        If oCtl.OLEFormat.ProgID = "Forms.TextBox.1" Then
                Set oTB = oCtl.OLEFormat.Object
                    MsgBox ("Shape>TextBox=" + oTB.Name)
        End If
    Next
    ----------------------------------------------------------------------
    ....I a get an error message "Error 91: Object variable or With Block variable not set". After digging through the documentation it indicates this should work. Any suggestion from someone experienced with Word VBA Active X text blocks would be appreciated.


    Tuesday, March 27, 2012 8:57 PM

Answers

  • The solution that seems to work is below, thanks for the help.

    For Each oCtl In Documents(NewFileName).Shapes
        If oCtl.Type = msoOLEControlObject Then
                If oCtl.OLEFormat.ProgID = "Forms.TextBox.1" Then
                     Set oTB = oCtl.OLEFormat.Object
                     MsgBox ("Shape>TextBox=" + oTB.Name)
               End If
        End If
    Next

    • Marked as answer by coltsrcul Tuesday, March 27, 2012 11:18 PM
    Tuesday, March 27, 2012 11:17 PM

All replies

  • Does your document contain other shapes that aren't ActiveX controls? Such shapes don't have an OLEFormat property.

    You can check whether the shape is an ActiveX control:

        For Each oCtl In Documents(NewFileName).Shapes
            If oCtl.Type = msoOLEControlObject Then       
                If oCtl.OLEFormat.ProgID = "Forms.TextBox.1" Then
                    Set oTB = oCtl.OLEFormat.Object
                    MsgBox ("Shape>TextBox=" + oTB.Name)
                End If
            End If
        Next oCtl


    Regards, Hans Vogelaar

    Tuesday, March 27, 2012 9:25 PM
  • It probaly has to do with how you have oTB declared. I took your code and made the slight alterations that you can see and it ran fine.

    Sub FindTextBox()
        Dim oCtl As Shape
        Dim oTB As Object
        For Each oCtl In ActiveDocument.Shapes
            If oCtl.OLEFormat.ProgID = "Forms.TextBox.1" Then
                Set oTB = oCtl.OLEFormat.Object
                    MsgBox ("Shape>TextBox=" + oTB.Name)
            End If
        Next
    End Sub


    Kind Regards, Rich ... http://greatcirclelearning.com

    Tuesday, March 27, 2012 9:30 PM
  • Hans is correct! Without a check on the type of shape, the routine fails.

    Kind Regards, Rich ... http://greatcirclelearning.com

    Tuesday, March 27, 2012 9:40 PM
  • Yes, I'm pretty sure it does. Though I'm not sure how to determine what items are shapes other than what I myself have added (I'm new to VBA for Word).

    Tuesday, March 27, 2012 9:44 PM
  • The Type property of a Shape tells you what kind of shape it is. See MsoShapeType Enumeration for a list of possible values.

    oCtl.Name might also provide a clue.


    Regards, Hans Vogelaar

    Tuesday, March 27, 2012 10:00 PM
  • Rich, I left out the the definitions in the posting, your suggestions is not helpful for my issue.


    • Edited by coltsrcul Tuesday, March 27, 2012 10:51 PM
    Tuesday, March 27, 2012 10:08 PM
  • Hans, yes looking at oCtl.Name and oCtl.Type is providing me some clues. More to following when I get somewhere.
    Tuesday, March 27, 2012 11:00 PM
  • The solution that seems to work is below, thanks for the help.

    For Each oCtl In Documents(NewFileName).Shapes
        If oCtl.Type = msoOLEControlObject Then
                If oCtl.OLEFormat.ProgID = "Forms.TextBox.1" Then
                     Set oTB = oCtl.OLEFormat.Object
                     MsgBox ("Shape>TextBox=" + oTB.Name)
               End If
        End If
    Next

    • Marked as answer by coltsrcul Tuesday, March 27, 2012 11:18 PM
    Tuesday, March 27, 2012 11:17 PM