View Full Version : Solved: Write to Word DOCUMENT Textbox
LondonVirus
07-11-2010, 08:48 PM
Hi,
I have been looking all over the net :banghead: 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. :dunno
Thanks for reading and for your help
Tinbendr
07-12-2010, 03:20 AM
Welcome to VBA Express!
I assume you mean a textbox on the document page.
A$ = "Hello World!"
ThisDocument.TextBox1.Text = A$
gmaxey
07-12-2010, 05:44 AM
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:
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
LondonVirus
07-12-2010, 06:47 AM
Thanks guy :thumb, 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:
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary) but I am not sure if I am right so far...?
Thanks once again for your help :bow: :help
Tinbendr
07-12-2010, 08:25 AM
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.
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
gmaxey
07-12-2010, 08:44 AM
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("MyTargetTextBox").TextFrame.TextRange.Text = Me.TextBox1.Text
fumei
07-12-2010, 12:36 PM
Or, if the footer has only the one textbox (likely I would think):
Sub IntoFooterTextbox()
ActiveDocument.Sections(1).Footers(1) _
.Range.ShapeRange(1).TextFrame.TextRange = Me.TextBox1.Text
End Sub
Assuming of course the footer is indeed Primary - Footers(1).
LondonVirus
07-12-2010, 02:59 PM
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":banghead:
MsgBox (ActiveDocument.Sections(1).Footers(1).Range.ShapeRange.Count)
another issue I am having, which I can't understand is that
MsgBox (ActiveDocument.Sections(1).Footers.Count)
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?
gmaxey
07-12-2010, 03:32 PM
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):
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.TextRange.Text = "I am Target 1"
ActiveDocument.Sections(1).Footers(1).Shapes("MyTarget2").TextFrame.TextRange.Text = "I am Target 2"
End Sub
gmaxey
07-12-2010, 03:38 PM
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
LondonVirus
07-12-2010, 04:11 PM
Thanks Greg, the footer counter makes sense now, silly me :dau:
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
gmaxey
07-12-2010, 04:27 PM
You can look at: http://gregmaxey.mvps.org/VBA_Basics.htm
Good luck.
LondonVirus
07-12-2010, 04:55 PM
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...
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Shapes(1).Name = "Address_TextBox"
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Shapes("Address_TextBox").TextFrame.TextRange.Text = "test"
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
gmaxey
07-12-2010, 05:33 PM
Maybe you could attach the troublesome document.
LondonVirus
07-12-2010, 05:40 PM
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 :help
LondonVirus
07-12-2010, 05:54 PM
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 :dunno
Tinbendr
07-12-2010, 06:34 PM
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".
LondonVirus
07-12-2010, 06:41 PM
Tinbendr (http://www.vbaexpress.com/forum/member.php?u=2549),
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
LondonVirus
07-12-2010, 07:58 PM
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.
ActiveDocument.Sections(1).Footers(1).Shapes(4).TextFrame.TextRange.Text = "test"
I would like to thank you all for all your help :clap:
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.