Computor Companion Logo BBBOnLine Reliability Seal
How To

Pass Repeated Information in a Word Document

by Dian D. ChapmanProtected by Copyscape. Do not copy.

Suppose you have a document that contains a person's name or a form letter that you need to regularly change to insert the make of a specific product or an account number. If you are manually typing that information in several locations throughout your letter or a contract, you're wasting time. If you have ever had to type the same information over and over in a Word document or want to have information passed into various locations throughout a Word form, this article is for you. You can add special fields to a master version of that document so that, when you enter the information into the first location it is inserted into all the other locations in your document.

Depending on your skills, you can handle this type of situation in several ways. In this article, I give you a few tricks that let you link up information in different ways, so whatever you type in one spot is automatically passed to other areas in your document.

First, I show you how to set up a simple document where you share the information by updating fields yourself. Then I look at how to do this task with a form so the update takes place automatically. And finally, I show you how to use pop-up input boxes to pass one piece of information throughout a document. You can choose the solution that best fits your skills.

Updating Bookmark Fields

Figure 1 shows a simple sample document, but you can pretend that it's a big contract. You need to enter the client's name in several locations throughout the document. I can't tell you how many times I've seen a document like this and watched as a person manually moves from location to location to replace the word "Client Name" with the actual person's name.


Figure 1.

Rather than just having ordinary text holding the locations you need, instead you can use text that is actually a REF field code. Using this field, when you replace and update the first instance of the required name or phrase, a field update causes that same information to instantly pass to each of the other locations.

First you set up your document with the first instance. You need a bookmark name in the document to hold the spot open and to avoid getting a confusing error. You can use the term "Client Name" to add that marker in the first instance. In the document in Figure 1, first you move to the title of the document and enter Client Name. Then you select those words and wrap them in a bookmark to give those words a name. This name serves as a reference when you add the REF field code.

As you can see in Figure 2, you select the text and press Ctrl + I + K to open the Bookmark dialog box. It's important to properly name markers, so use the prefix of "bk" to indicate this is a bookmark name. Sure this little step may not seem important now, but when you get involved in using more complex codes, you'll see how important it can be to use a prefix to add a little hint so you remember what type of marker it is.


Figure 2.

Now move to the first location where this same information will be duplicated and replace the "Client Name" text with a REF field that references the bkClientName bookmark.

You should delete the "Client Name" words that are there already. In that same location, click to make sure the cursor knows where it should enter the field. Now choose Insert|Field and choose the REF field as shown in Figure 3.


Figure 3.

Notice that the bookmark "bkClientName" is in the list. If you didn't have some text already wrapped in this bookmark to add it to the master version of this document, it would not be available to choose in this list. Also set the Format to Title Case and choose to Preserve Formatting During Updates.

When you click OK and the field is entered into the document, at first glance it looks exactly the same as it did before, as you can see in Figure 4.


Figure 4.

You'll also notice that the words in the title have brackets around them. These brackets indicate it is a bookmark. To see this important visual aid, you need to choose Tools|Options|View and check the option to show Bookmarks. (To view a free video that explains setting Word's options, go to this link: http://www.mousetrax.com/TechCourses.html#samples, and click the Word Options sample.)

If you select the text you just entered with the field code and press the Field Toggle keystroke of Shift + F9, you see the words Client Name is now really a field code that references whatever is contained within the bkClientName bookmark (see Figure 5). That text just happens to be the text Client Name. You can press Shift + F9 to toggle the field code back into its results.


Figure 5.

Once you have this one field inserted, you can simply select this code and press Ctrl + C to copy it to your clipboard. (See this article for more information about copying and pasting text in a document: No Eating Paste.) Now move to the other instances of the Client Name text and press Ctrl + V to paste that code into those locations. This approach is faster than inserting the same code over and over using the dialog box.

When you need to use this contract document for a new client, you can go to the first bookmarked location, select the words Client Name and type over them with the name of the actual client. Note that it can be somewhat tricky to type over bookmarked text without accidently deleting the bookmark in the process. If that happens, select the client's name and reinsert the same bookmark name around the words by pressing Ctrl + I + K and typing bkClientName and clicking Add.

One last step will cause the clients name to be shared in all those places throughout the document. You need to select the entire document by pressing Ctrl + A. Now update the fields by pressing the F9 key. In Figure 6, you can see that the client's name (John Smith) is now inserted into all the locations. You also can see in the image that I've toggled one field and it's actually still just a REF field.


Figure 6.

Automatically Updating Links

If all that manual updating is too much work for you, you can simplify the process using form fields in your initial location. Then you can type the client's name into the form field and just press the Tab key. When you do, all of the linked locations instantly receive the same information you typed into that form field.

To set up this version of the same document, we need to do a few different things. First, rather than having text wrapped in a bookmark, you need to enter a form field in the first location where the information will be typed. To do this, display the Form Field toolbar by choosing Tools|View|Toolbars|Forms.

The first icon on the Forms toolbar represents the TextBox field, which is the one you'll need to click to insert that type of form field into the document.


Figure 7.

Once the form field is entered, you need to customize several Option settings for this form field. If you double-click the form field, Word opens the Options dialog box for that field. In Figure 8, you can see my settings.


Figure 8.

You can choose the Type and Maximum Length settings to meet your requirements. If you want to add some default text (which is a good idea) enter that in the Default Text input box. Make sure you enter the same bookmark name you have referenced in the REF code. So if you used bkClientName, enter that name here for the bookmark name too. Finally, to make the code work automatically, check the option for Calculate On Exit. This option tells the field to update itself when the user presses Tab to exit out of this field.


Figure 9.

Now there's just one more step to finalize the document. You need to Protect it so the document is locked for form use. To protect the document, choose Tools|Protect Document|Forms and click OK. If you are using Word 2003, the process has changed a bit. You need to choose Filling in Forms and click the Start Enforcing Protection button, as shown in Figure 10.


Figure 10.

Now when you reopen this document in the future, you just need to type the client's name into the form and press the Tab key. The information is then passed to all the REF fields in the document, as you can see in Figure 11.


Figure 11.

Make it Slick by Adding VBA

For those of you brave enough to venture into the wonderful world of document automation a little further, here's how you can add a little code to your document to enhance it. Note that I run through these steps rather quickly. If you want to learn more about automating forms with VBA, you'll find a pile of other free articles on my web site at http://www.mousetrax.com/techpage.html#autoforms. And if you want to quickly get much better at using VBA to automate documents, you might want to check out my very popular course: Word AutoForms and Beginning VBA.

You can start with the same document and leave the REF codes as they are. But rather than entering text wrapped in a bookmark or a form field, you need to change it to just a plain old single bookmark. Press Ctrl + I + K to enter your bkClientName bookmark, but this time don't select any text. Just enter the bookmark in the location where you want the first instance.

One confusing issue is that because you don't have any information being referenced by the bookmark, your REF forms will seem to disappear because there's no information within them. To check, you can select the document and toggle the form fields to see that your fields are still there. Also notice the single bookmark in the title looks like a large I-beam.


Figure 12.

Now it's time to venture into the Visual Basic Editor (VBE) to write some code. Press Alt + F11 to open the VBE. Once inside, be sure to select the project document you're using, so you don't end up adding the code in the wrong document. Choose Insert|Module to insert a new code module to hold your VBA code.


Figure 13.

Once the module is inserted, double-click it to open the code window. In that window, enter the following code. Refer to Figure 14 to make sure you've done it correctly. Just one missing character or space and you can get an error and your code won't work. So type it exactly as written here or copy the code below and paste it into your VBE.

Option Explicit
Sub RequestInfo()
  'declare your variable
  Dim strClientName As String
  'set variable equal to whatever user types into input box
  strClientName = InputBox("Enter Client's Name:")
  'declare a range
  Dim bkRange As Range
  'set the range equal to your current bookmark
  Set bkRange = ActiveDocument.Bookmarks("bkClientName").Range
  'pass in the info now contained in the variable (what the user typed)
  bkRange.Text = strClientName
  'wrap the same bookmark around this text
  ActiveDocument.Bookmarks.Add "bkClientName", bkRange
  'select all text
  Selection.WholeStory
  'update fields
  Selection.Fields.Update
  'move cursor to deselect
  Selection.MoveUp Unit:=wdLine, Count:=1
End Sub


Figure 14.

The red comment text in Figure 14 explains what each step does. First an input box appears, so the user can type in the client's name. After the user clicks OK the information typed in the box is inserted into the location where you entered the bookmark. The bookmark will be rewrapped around the text. Then the document text is selected and the fields are updated. These steps are literally the same ones you did manually in the first step. The only difference is that now the VBA code does it all in a nano-second.

There is one final step. To get the ball rolling, you need to tell the document to show the form when you open the document. Normally, you would save this document as a template and save the "show" code in the Document_New event. Then when you create a new document from the template, the input box appears. However, you may choose to save this as a document. In that case, you'll need to add it into the Document_Open event. I've added it in both events, so you can choose how you want to activate the code.

Option Explicit
Private Sub Document_New()
  Module1.RequestInfo
End Sub
Private Sub Document_Open()
  Module1.RequestInfo
End Sub

This code must be inserted into the ThisDocument code module that every document has. Refer to Figure 15 to make sure you put it in the right place. Just double-click ThisDocument to open that module.


Figure 15.

Depending on the way you decide to use it you need to save the file as a document or a template. Note that you don't need to protect the document now. You can just reopen it or create a new document from it. When you do, you should see the input box automatically appear asking you for the client's name.


Figure 16.

Type in a name and click OK. The code runs and processes the information as needed. The result is that your document gets the same name spread throughout the document, instantly as you can see in Figure 17.


Figure 17.

And if you attempt the VBA code and feel frustrated if you can't get it right...remember this popular programmer's quote...

"Give a man a program and frustrate him for a day. Teach a man to program and frustrate him for a lifetime!"

Enjoy :-)

Like the articles in Computor Companion? Check out our computer tips books!

logical tips

Logical Tips for Mastering Your Computer:
Quick Shortcuts, Tips, Tricks, and Techniques to Help You Use Your Computer More Effectively

Go from Computing Newbie to Power User!

Read about this book on Amazon
logical tips internet

Logical Tips for Mastering the Internet:
Quick Shortcuts, Tips, Tricks, and Techniques to Help You Use the Internet More Effectively

Go from Internet Newbie to Expert!

Read about this book on Amazon
logical tips office

Logical Tips for Mastering Microsoft Office:
Quick Shortcuts, Tips, Tricks, and Techniques to Help You Use Microsoft Office More Effectively

Don't Let Microsoft Office Drive You Crazy!

Read about this book on Amazon
logical tips office

Logical Tips for Mastering Microsoft Windows:
Quick Shortcuts, Tips, Tricks, and Techniques to Help You Use Microsoft Windows More Effectively

Combat Windows Weirdness!

Read about this book on Amazon

Did you like this article? Let us know!
Please click here to send feedback on this article.
NOTE: If you have computer-related questions, you must post them on our forum.
We do not answer computer questions via email.

Don't Miss Our Weekly Publishize Newsletter!
Learn how to create books, web sites, and info products
Email
Name


Check Out
Our Books!



This site powered by the Logical Web Publisher (TM): Fast, easy, and affordable content management