Consulting

Results 1 to 19 of 19

Thread: Solved: Write to Word DOCUMENT Textbox

  1. #1

    Exclamation Solved: Write to Word DOCUMENT Textbox

    Hi,

    I have been looking all over the net on how to write data from a form to a textbox that is on the actual documet, all I seem to find is how to write data to the form's textbox.

    So far I have the form data saved to a variable, now I need to write/insert it to a textbox that is already part of the document template, but don't know how.

    Thanks for reading and for your help

  2. #2
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    Welcome to VBA Express!

    I assume you mean a textbox on the document page.

    [vba]A$ = "Hello World!"
    ThisDocument.TextBox1.Text = A$[/vba]

    David


  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Assuming by "Textbox" that you mean a Text box drawing object located in the document. When these are created they will have both an index (order in which they were created) and a name. Further assuming that you want to write to say the fourth box or one named "Text Box 4" you would use something like this in the UserForm command button event:


    [VBA]Private Sub CommandButton1_Click()
    ActiveDocument.Shapes(4).TextFrame.TextRange.Text = Me.TextBox1.Text
    'or
    ActiveDocument.Shapes("Text Box 4").TextFrame.TextRange.Text = Me.TextBox1.Text
    Me.Hide
    End Sub
    [/VBA]
    Greg

    Visit my website: http://gregmaxey.com

  4. #4

    Question

    Thanks guy , That's exactly what I ment...


    Now how about if the text box is in the document's footer? (2 questions)

    1) Does it follow the document's index number and naming range or does it have its own index and naming range?

    2) How do I access the text box in the footer?

    For part 2 I got to something like this:
    [vba]ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary)[/vba] but I am not sure if I am right so far...?


    Thanks once again for your help

  5. #5
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    Yes, you are correct. (My first example was using 2003.)

    For 2007, use something like this IF you are using textboxes from the Developer Tab. If you are using Drawing textboxes, you'll need to use Greg's example.
    [vba]Sub test1()
    With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
    With .ContentControls(1)
    .Tag = "Tag Names 1"
    .Title = "Title Textbox 1"
    .Range.Text = "This is a text of Textbox 1."
    End With

    With .ContentControls(2)
    .Tag = "Tag Names 2"
    .Title = "Title Textbox 2"
    .Range.Text = "Textbox 2 is testing this code."
    End With

    End With
    End Sub[/vba]

    David


  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Your best bet might be to explicitly name the target text box by selecting it and then running something like:

    Selection.ShapeRange.Name = "MyTargetTextBox"

    The writing to that specific target using something like:

    ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Shapes("MyTargetT extBox").TextFrame.TextRange.Text = Me.TextBox1.Text
    Greg

    Visit my website: http://gregmaxey.com

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Or, if the footer has only the one textbox (likely I would think):[vba]
    Sub IntoFooterTextbox()
    ActiveDocument.Sections(1).Footers(1) _
    .Range.ShapeRange(1).TextFrame.TextRange = Me.TextBox1.Text
    End Sub
    [/vba]Assuming of course the footer is indeed Primary - Footers(1).

  8. #8

    Question

    Guys thanks for your help, the version of Ms Word I am using is 2003, and the version of office my VBA script will be running on is 2003, I am under the impression that you guys beleave I am on 2007.

    I tried the codes above and they are not working for me, and for some reason, even thought there is 2 text box in the document's footer, when I run the code bellow, the text box shous "1"

    [vba]MsgBox (ActiveDocument.Sections(1).Footers(1).Range.ShapeRange.Count)[/vba]
    another issue I am having, which I can't understand is that

    [vba]MsgBox (ActiveDocument.Sections(1).Footers.Count)[/vba]

    returns "3" when I only have 2 footers for the whole document, by using the "Different First Page" option in "Page Setup" so the second code should return "2" isnt it?
    Last edited by LondonVirus; 07-12-2010 at 03:34 PM.

  9. #9
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Are you sure that both text boxes are anchored to the footer? You can have a textbox that appears to be in the footer but is anchored in the maintext story.

    If I create a new document and add two textboxes anchored to the footer I can run this code with no problems (using Word2003):

    [VBA]Sub CountThem()
    MsgBox (ActiveDocument.Sections(1).Footers(1).Range.ShapeRange.Count) 'Returns 2
    End Sub
    Sub NameThem()
    ActiveDocument.Sections(1).Footers(1).Shapes(1).Name = "MyTarget1"
    ActiveDocument.Sections(1).Footers(1).Shapes(2).Name = "MyTarget2"
    End Sub
    Sub WriteTextToThemScratchMacoII()
    ActiveDocument.Sections(1).Footers(1).Shapes("MyTarget1").TextFrame.TextRan ge.Text = "I am Target 1"
    ActiveDocument.Sections(1).Footers(1).Shapes("MyTarget2").TextFrame.TextRan ge.Text = "I am Target 2"
    End Sub
    [/VBA]
    Greg

    Visit my website: http://gregmaxey.com

  10. #10
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Every section of every document has three footers regardless if you use them or not (primaryFooter, evenPageFooter, firstPageFooter)

    ActiveDocument.Sections(1).Footers(1).Range.ShapeRange.Count is the same as:

    ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.ShapeRange. Count
    Greg

    Visit my website: http://gregmaxey.com

  11. #11

    Thumbs up

    Thanks Greg, the footer counter makes sense now, silly me

    Your help means alot to me as this project has been causing me problems for ages

    I will try to apply this code to my project and see how it goes


    Any of you knows where I can find some tutorials for VBA Word? all I seem to find online is VBA Excel and so on, but hardly any word ones, any reason why?

    Thanks again

    LondonVirus

  12. #12
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    You can look at: http://gregmaxey.mvps.org/VBA_Basics.htm


    Good luck.
    Greg

    Visit my website: http://gregmaxey.com

  13. #13

    Question

    Guys, I know I may be staring to be a pain, and I am sorry...

    But after having used the code above, moded to be used in my project, I receive "Run-Time Error 5917" which apparently means "This object does not support attached text", but I should be able to add text to a "text" box lol...

    [vba]ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Shapes(1).Name = "Address_TextBox"
    ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Shapes("Address_T extBox").TextFrame.TextRange.Text = "test"
    [/vba]

    By the way I have sorted the text box count issue, there is some other text in the footer which I thought I had added in a text box too, but I had not, it was just plain footer text, so there is only one text box in the footer, and it's the one I want to add text to, this document is 10 pages and very complex, full of tables, and I could not remeber that there was only 1 text box in the footer, sorry guys lol

  14. #14
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Maybe you could attach the troublesome document.
    Greg

    Visit my website: http://gregmaxey.com

  15. #15
    Due to privacy I can't, as the document is not private, but a surveyour's document, which I was asked not to make public, I hope you can still help me

  16. #16
    Greg, I am confused, I run your VBA code in a new doc and it works, but not in my main one, I suppose I will have to try and sort it out my self

  17. #17
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    Quote Originally Posted by LondonVirus
    I am under the impression that you guys believe I am on 2007.

    I tried the codes above and they are not working for me,
    Since you have not told us what version you are using; since you have not uploaded a sample, we are just guessing. It's like leaving a note on your car for your mechanic and telling him, "It doesn't run right".

    David


  18. #18
    Tinbendr,

    I mentioned after that the version is 2003...

    Anyway I know what the issue is, I have a "mess" with the sections, got to change those, the reason why word cannot add text to the object is that the object does not exist/or the wront object is selected, I am looking into how to change the section number so that I only have section 1 for the first page, and section 2 for the rest...

    if you know a quick way to do so, please let me know...


    Thank you again

  19. #19

    Cool [RESOLVED]

    Greg,

    Thank you for your help, your original code for MS Word 2003 did the job after some testing and mods, the reason why it wasnt working is that the "Shapes. Count" included the sum of header and the footer's shapes.

    Therefore me using the "footer(1).Shapes(1)", knowing that I only had 1 text box in the footer, was not working as the code was trying to add text to the first Shape, which was an image in the header side of things and obviesly not able to add text to it, causing the "Run-Time Error 5917" (This object does not support the attached text).

    After having counted all the shapes in header and footer, the shape at the bottom was shape number "4" therefore the following code did the job.

    [VBA]ActiveDocument.Sections(1).Footers(1).Shapes(4).TextFrame.TextRange.Text = "test"[/VBA]

    I would like to thank you all for all your help

Posting Permissions

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