Results 1 to 14 of 14
  1. #1
    Star Lounger
    Join Date
    Jun 2004
    Location
    Lambertville, New Jersey, USA
    Posts
    82
    Thanks
    0
    Thanked 0 Times in 0 Posts

    enumerate Text Boxes in word doc (Word 2002)

    I need to populate test boxes in a word doc using VB. The problem is that the text box names are variable. I have the names they're "supposed to be" stored in an array. I can't (don't know how) to reference an object name using the value of a variable. i.e. I want to read the name of the text box from the array and set a value to it. like this... VarNameArray(1) = "txtLastName" VarNameArray(1).value = "Smith" (i.e. evaluated as: txtLastName1.value = "Smith") - I thought I would enumerate the text boxes contained in the word doc and check their name property and set the value based on the name but I can't figure out how to enum the text boxes in a word doc.
    Hope this isn't too confusing. Any help would be appreciated.
    Thanks,
    Lance

  2. #2
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: enumerate Text Boxes in word doc (Word 2002)

    Can you do something with this (untested) code
    <code>Dim shp as Shape
    For each shp in ActiveDocument.Shapes
    If shp.Type=msoTextBox Then
    <font color=448800>' Put code that does things to the text box here</font color=448800>
    End If
    Next shp</code>

    StuartR

  3. #3
    Star Lounger
    Join Date
    Jun 2004
    Location
    Lambertville, New Jersey, USA
    Posts
    82
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: enumerate Text Boxes in word doc (Word 2002)

    I will try it and let you know.
    Thanks for responding so quickly.

    Lance

  4. #4
    Star Lounger
    Join Date
    Jun 2004
    Location
    Lambertville, New Jersey, USA
    Posts
    82
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: enumerate Text Boxes in word doc (Word 2002)

    No luck.
    The "For Each" loop never executed.
    No "Shapes" in the doc?

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: enumerate Text Boxes in word doc (Word 2002)

    Activate the Immediate window in the Visual Basic Editor (Ctrl+G)
    Type ? ActiveDocument.Shapes.Count and press Enter.
    If you get 0 as result, type ? ActiveDocument.InlineShapes.Count and press Enter.
    If you get 0 again, something strange is happening - if there are text boxes in the document, at least.

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: enumerate Text Boxes in word doc (Word 2002)

    Strange, a text box always counts as a Shape for me, never as an InlineShape, even if I make it inline. Are you sure you're talking about text boxes?

  7. #7
    Star Lounger
    Join Date
    Jun 2004
    Location
    Lambertville, New Jersey, USA
    Posts
    82
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: enumerate Text Boxes in word doc (Word 2002)

    I got 0 shapes and 18 inlineShapes
    I used the debug window and displayed the .type property of each. I got 17 type "5" must be text box and 1 type "1" Excel Worksheet

    How can I get the name of the text boxes and then assign a value to the corresponding text box in the word doc?

  8. #8
    Star Lounger
    Join Date
    Jun 2004
    Location
    Lambertville, New Jersey, USA
    Posts
    82
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: enumerate Text Boxes in word doc (Word 2002)

    Definitely Text Boxes.
    Do you still have the example .doc file I sent you a couple days ago? That should have one text box and one excel sheet.

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: enumerate Text Boxes in word doc (Word 2002)

    Ah, I was thinking of Word text boxes - the kind you create by selecting Insert | Text Box. You mean the ActiveX control created from the Control Toolbox.

    Dim shp As InlineShape
    For Each shp In ActiveDocument.InlineShapes
    If shp.Type = wdInlineShapeOLEControlObject Then
    ' do something with the control
    shp.OLEFormat.Object.Value = "This is a test"
    End If
    Next shp

  10. #10
    Star Lounger
    Join Date
    Jun 2004
    Location
    Lambertville, New Jersey, USA
    Posts
    82
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: enumerate Text Boxes in word doc (Word 2002)

    That worked to a point. It recognized the text boxes and performed the loop but it also attempted to run the code on a command button I have in the doc since the cmdBtn is also a member of "wdInlineShapeOLEControlObject". Also, there does not appear to be a .Name property on InlineShapes which I need. Perhaps I need a whole new approach.
    In a nut shell, here's what I need to do.
    I need to take values from a worksheet and place them in text boxes (or something else) in the doc. The text box name is variable and will also be read from the worksheet. Therefore, I need to read the name of the text box as well as the value to be assigned to it from the worksheet. There will be several so I'll be looping. Then I need to find the text box (by name?) in the doc and assign the value to it.
    Any ideas?
    Thanks for all your time.

  11. #11
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: enumerate Text Boxes in word doc (Word 2002)

    An InlineShape as such doesn't have a name, but the control does:

    shp.OLEFormat.Object.Name

    You can also find the type of control:

    shp.OLEFormat.ProgID

    This returns a string, "Forms.TextBox.1" for a text box, "Forms.CommandButton.1" for a command button etc.

  12. #12
    Star Lounger
    Join Date
    Jun 2004
    Location
    Lambertville, New Jersey, USA
    Posts
    82
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: enumerate Text Boxes in word doc (Word 2002)

    Great! It's working.
    Thanks so much. Once again, you've gotten me through a tough spot.

    Where could I have found the info on .OLEFormat.Object.Name and .OLEFormat.ProgID
    I can't find these anywhere in my on-line help.

    You and this board are great.
    Thanks,
    Lance

  13. #13
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: enumerate Text Boxes in word doc (Word 2002)

    If you click in OLEFormat or in ProgID and press F1, you should get help on it.

    Information about OLEFormat.Object is scarce, since it can be all kinds of objects. For tha same reason, IntelliSense doesn't work if you type a period after Object. I found that OLEFormat.Object.Name works by trial and error.

  14. #14
    Star Lounger
    Join Date
    Jun 2004
    Location
    Lambertville, New Jersey, USA
    Posts
    82
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: enumerate Text Boxes in word doc (Word 2002)

    Hans;
    Thanks for the information.
    Microsoft doesn't make it easy. It shouldn't be this hard to find information.

    Thanks again for all your time,
    Lance

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •