One of the problems with headers/footers is that they are in a different layer than the document content itself. That means that if you have any fields in your header/footer and need to update them, you have to go into the header/footer view in order to select the content and hit F9 to update those fields. Sometimes this can be a problem, particularly if you're trying to create a very user friendly template for users who don't understand how headers/footers work.
In this article, I'll show you how you can use a little automation to handle the job for you or your users. Note that you may want to check out this Computor Companion article first, to make sure you understand how headers and footers work: Understand Word's Headers and Footers
The Problem
Let's assume we're creating a Word form. At some point, we have a form field where the user will insert some dynamic information (information that can change or be different for each user). Simple enough so far. But we also want that same information to also go up into the header.
Without any type of automation, the user would have to either open the header and insert this same information in there, or at the least, know how to open the header, select all and update fields. Worse, if this is a form, they would have to also know how to unlock the form in order to get into the header/footer area. Not only is that a lot of work, but will most likely defeat the purpose of having a locked form in the first place. So let's make it easier for the user.
The Solution
Actually, the solution is pretty simple. You just need to record a macro that unlocks the form, goes into the header, selects the content, updates the fields, closes the header and relocks the form. Then you add that macro into the Run Macro on Exit option in the form field.
But for the benefit of those who are currently cursing me for saying how simple that is when they don't understand how to do this...let's go through all the steps to make sure everyone who wants to learn how to do this...can learn it.
Setting Up the Template
Okay, create a new document by clicking File > New. Immediately, hit Alt + F + A for File > Save As. Click the Save As Type dropdown in the Save As dialog box and choose to save this file as a template.

Note!
If you don't know why we are saving this file as a template, read this article: Normal.dot Template-Explained.
Okay, our template is started. Now we'll pretend that you have created this wonderful form that you'll have users fill out on their computer and you need to add one last field...for the user to enter their name. Make sure your Forms toolbar is turned on by clicking View > Toolbars > Forms. Then click the Text Form Field icon to add a form field into your document.
It is a very good idea not to leave the default field names, because they are too generic and can too easily cause confusion when working to automate a form. So double click the form field and change the Text1 default form field bookmark name to something more meaningful, such as bkName.

Now we need to move into the header to insert the field that will be linked to whatever the user types into this form field. Click View > Header and Footer to move into the header/footer layer of the template. Hit Ctrl + R to right align the information we will now add.
Tip!
Why right align? Because the information we will ultimately have displayed here is the name of the person filling out the form. This will be important information. If we left aligned it and later decided to put these forms into a 3-ring binder, it would be more difficult to look to the inside of the binder to see the name, versus being able to more quickly flip through the pages to see the names on the outer edge of the pages. And yes, you should also stuff your page numbers on that outer edge. Why make things harder?
Back to the form...click Insert > Field and scroll down the list to the REF (Reference) field. Click to select that field.
And now you'll see a perfect example of one reason why we changed the default form field name. Notice the bookmarks from which you need to choose. Granted, you'll probably just have the one. But what if you didn't. You would have bookmarks like: Text1, Text2, Text3. Sure, the troublemakers in the group will say..."easy, the Name bookmark is the first field I put into the template, so it'll be Text1." And that may very well be. But what if you moved things around as you were designing it...or didn't put them into the document in the order that they appear. At most, you'll be making a good guess about which is the correct bookmark. And when it comes to programming, you shouldn't be guessing. Why make things harder? If you took the time to rename the bookmarks to a meaningful name, your job here will be easy. Click on the name form field bookmark and be on your way. Although you'll probably also want to check the Preserve formatting during updates checkbox.

Tip!
One other thing you may want to do is change the MERGEFORMAT option, which will be added into the field, to CHARFORMAT. Mergeformat will keep the paragraph formatting. But charformat will keep the character formatting of the first word. People sometimes wonder why they have fields and some of the formatting gets messed up, i.e., the first word stays bold as they want it, but the other words in the field end up in another format. Use charformat to ensure that the entire reference will be in the same formatting as the first word.
Once you've added the field, you won't see anything yet because nothing has yet been added into the form field. But if you hit Ctrl + A to select all the content of the header, you can then hit Shift + F9 to toggle the field into code to see what it looks like. Yours should look similar to this: { REF bkName \* charFORMAT } and as shown below.

Note!
By the way, if you hit F9 to update your field and you should see this message: Error! Reference source not found., it means that you have a typo because the field you referenced is not in the document. Granted, that error is unlikely in this case because we selected the bookmark from the list of available bookmarks, so the worst you could do is have selected the wrong one, which will insert different information. But if you created the field manually or ever see this error in another document...now you know what it means: the information you want in the document isn't in the document at this point.
Make sure you've toggled the field back to a field which you can do by hitting Shift + F9 again, or just hit F9 to update it. Then close out of the header/footer by either clicking Close on the Header/Footer toolbar that appeared when you moved into that area, or just double click on an area of the document...that is outside of the header area.
Automation Code
Now we'll create some VBA code. First we'll record a quick macro to get much of the code we need done for us, then we'll go into the VB editor (VBE) and tweak things a bit.
Click Tools > Macro > Record New Macro or just double click the REC button at the middle/bottom of your screen.

The Macro Recorder dialog will open. Give it a meaningful name such as UpdateHeader. But here's the most important part...make sure you click the drop down and put the macro inside your current template! This is one of the biggest mistakes that user's make...they don't pay attention to where they put things, so they lose things! You can move it where it belongs later if you make this mistake, but why make things harder?
After clicking OK, what you do will be recorded into this macro.
- Click View > Headers and Footers.
- You'll default into the Header. Hit Ctrl + A to select all the text.
- Hit F9 to update the field. (Nothing will happen yet since there is still no information in the field.)
- Click Close on the Header/Footer toolbar.
- Stop the Recorder...by either clicking the Stop button on the recording toolbar or by clicking Tools > Macro > Stop Recording.
If you have anything in your footer, it would probably be best to create a separate footer macro, so you can call that in code, separately, when needed.
Hit Alt + F11 to move into the VBE.
You should see your project on the left. A default code module will have been inserted, which will be called NewMacros because you recorded the macro. I prefer to change that name to modMain because I often have several modules in a project. This isn't necessary, but if you want to do that, click on the module. In the Properties window, below the Project window, click in the Name field and enter the new name and hit enter. The module name will now change.

Note!
If you don't see the Project or Properties windows, click the View menu and click on the necessary windows.
You will now need to add some handwritten code. Actually, you could have also recorded in the form unlocking and relocking steps into your code, but what if you want to use this code later and there is a different password? It is always better to separate out different parts of code. This not only allows you to reuse parts of code, but when you get into more complex coding, it's the proper way to do it...make your code modular.
So move below the recorded code that is current in the window on the right and type in the code below.
Sub ToggleFormLock()
'**********************
'code by dian@mousetrax.com
'**********************
'PURPOSE
'quickly checks dot state and toggles opens/closes as
'needed to enter data. Activatepassword portion of code if
'a password is being used.
'**********************
'if locked, unlock, else lock
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
ActiveDocument.Unprotect ' Password:="aPassword"
Else
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True ', Password:="aPassword"
End If
End Sub
Feel free to add your own name as the coder.
The red text that starts with an apostrophe is a comment. It is information about the code that will not run or it is code that has temporarily been turned off. It's always important to add notes to yourself in your code. Trust me, six months from now you'll appreciate those notes if you have to come back and figure out what you were doing.
But also, in this case, some of the code has been turned off. If you have a password in your form, you'll want to reactivate that code. Remove the apostrophe in those lines and move the code back into place...leaving one space in the first one and moving the comma after the word True in the second one. Obviously, you'll want to change the password to your own, too.
Nearly done. Now you just need to call this Toggle code before and after the code you recorded. Add the name of this new macro into the recorded macro...just after the name of that macro, but before the code starts. Add it again near the end, just before the End Sub code line, as shown below.

When this code is called to run, it will first jump down and run the ToggleFormLock code, which will unlock a locked form. Then it will open the header and update the fields. Then the toggle code will be called again, but this time it will lock the unlocked form.
And because you now have this toggle code separated from the other code, you can easily call this code anytime you need to lock/unlock a form, by just moving it into other VBA projects.
Remember to hit Ctrl + S to save this code within your template.
The final step to make this code work with your form is to add this macro into the Exit option of the form field. Go back to your form, double click the Name form field and click the Exit dropdown. Select the UpdateHeader macro (or whatever you named it).

You can now lock your form by clicking Tools > Protect Document > Forms. If you're using 2003, you'll click Tools > Protect Document and then click the Allow only this type of editing in the document option in the Task Pane that will appear. Then choose the dropdown below that option and choose Filling In Forms and click the button to Start Enforcing Protection.
If you want a password, remember to add it here and also activate the password code that was commented out. Then save and close your template.
To test it, click File > New. Select your newly created template. A new form document will be created from your master template. Type your name into the field and hit the tab key.
If you did this all correctly, you should see your name in the header. If you get an error...you now get to learn all about debugging!
Good luck!
Dian Chapman is a Word automated forms expert. If you want to learn more about how to create automation in your documents, be sure to check out her list of free tutorials here: http://www.mousetrax.com/techpage.html#autoforms. Or, if you want to get started creating even more sophisticated automation, be sure to check out her Word AutoForms and Beginning VBA training course, here: http://www.mousetrax.com/techcourses.html. But if you'd rather just save time and money with automated documents, but don't want to bother learning how to do all this, you can request a quote to have your documents automated or view the many solution ideas for automation at Dian's consulting site, here: http://www.mousetrax.com/consulting.

|