vba code for text boxes

S

Savio

ive got a word doc in which i have added some text boxes. ive also
got
a user form which enables text input into text boxes within the user
form.
wat set of codes can i use to take the input values and put them into
the text box on the word doc?
 
G

Gordon Bentley-Mix

Savio,

The answer depends on what you mean by "text boxes". If you mean text-type
formfields then something like the following should do the job:

ActiveDocument.FormFields("TextBox1").Result = UserForm1.TextBox1.Value

This assumes the name of the bookmark associated with the formfield is
"TextBox1" and the TextBox control on your UserForm is also called
"TextBox1". However, there is no requirement that the bookmarks and TextBox
controls have the same name; you can call them anything you like. In
addition, the user of "UserForm1" is not strictly required. I only included
it to make it clear that the second "TextBox1" was referring to the TextBox
control on the UserForm.

Of course, if by "text boxes" you mean the _other_ kind - the one that's
created by clicking Insert | Text Box (in Word 2003 or earlier) - then the
process will be different. In this instance, the best method would be to
insert unique bookmarks into each of the text boxes and using something like
this:

ActiveDocument.Bookmarks("Bookmark1").Range.Text = UserForm1.TextBox1.Value

You may also want to include some error handling to ensure that the bookmark
exists before you try to write text into it. In addition, if you have a lot
of values to insert, you might want to look at writing a 'generic' procedure
that accepts arguments for the bookmark name and the text - perhaps like this:

Sub InsertBookmarkValue(BkmkName As String, ByVal InsertValue As String)
If ActiveDocument.Bookmarks.Exists(BkmkName) Then
ActiveDocument.Bookmarks(BkmkName).Range.Text = InsertValue
End If
End Sub

You would then call this as follows:

InsertBookmarkValue "Bookmark1", TextBox1.Value

Note that the above will work with bookmarks in any location in the
document, not just in text boxes.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top