Most businesses use form letters or standard documents, in which a few details need to be changed each time the document is created. The best way to deal with this is to save the document as a template. You can add form fields for the variable content to make the job of completing the document easier. But for the greatest control and ease of use, you can't beat the versatility of a VBA UserForm. A VBA UserForm is a custom dialog box, to which you can add all the features you would normally see in a regular Windows dialog box like text boxes, combo boxes, option groups, check boxes and more.
This three part tutorial shows you how to build an automatic document template for Microsoft Word. The template uses VBA to display a dialog box into which the user enters the necessary information. At the click of a button the information is transferred to the document and it's ready to print.
The first part of the tutorial deals with preparing the standard document. In the second part we design and build a VBA UserForm, and in part three we write the VBA code that powers the form.
Preparing the Standard Document
Your project might be a standard document such as a lease or contract, or a form letter such as a job offer or interview invitation. In this tutorial I use the example of an interview invitation.
The first task is to decide which parts of the document change each time, and which remain the same. If the document is a letter, for example, write it as if you were addressing it directly to an individual but, instead of entering any personal information, insert a bookmark.
For example, instead of writing "Dear Mr Green" you should write "Dear " and then enter a bookmark to define the place where the salutation "Mr Green" will be inserted from the UserForm. Before inserting your bookmarks you might find it useful to go to Tools > Options > View and tick the Bookmarks option. This ensures that you can see the position of a bookmark after you have inserted it.
Insert the Bookmarks
To insert a bookmark place your cursor where you want your inserted text to appear and go to Insert > Bookmark. This displays the Bookmarks dialog box. Type a name for your bookmark and click Add. Each bookmark in a document must have a unique name, and it makes sense to keep the names relevant and concise.
If you have opted to view bookmarks, as suggested above, you will see a grey marker denoting the position of the bookmark. If not, the bookmark will be invisible. This feature can be switched on and off at any time and does not affect the performance of the bookmark. You may prefer to have the bookmarks visible whilst you are creating the template, but hide them when you are using the document.
You don't need to leave any additional space for the text that will be inserted into your bookmark as this will happen automatically. If your inserted text will appear in the middle of a sentence, then your bookmark should have a space (or other suitable punctuation) on either side. If it is at the end of a sentence it should be followed immediately by the full-stop (period).
As you add bookmarks to your document they are listed in the Bookmarks dialog. This dialog also gives you the facility to delete bookmarks from your document, or to quickly go to a bookmark's position...
Bookmarks can be inserted as you type or you may prefer to type a sample document first, and then edit it to remove personalized text replacing each piece with a bookmark.
When designing out your document remember that, whilst the bookmark marker occupies only a small space, your inserted text will probably require more. This might affect the document's layout so take this into consideration if the layout is particularly important. Here's how the sample document looks after all the bookmarks have been inserted...
Make Use of Word Fields
Several kinds of self-updating information can be added to your document with the aid of Word fields. You can see that the letter contains a date. Don't type a date! If you do that it will have to be changed each time you use the template. Instead insert the date as a Word field. Place your cursor where you want the date to appear and go to Insert > Date and Time and choose the format you want. Make sure that the Update Automatically option is ticked and click OK to place the field in your document. This field will then display the current date whenever the document is opened (i.e. each time you create a new document using your new template).
Save the Document
Now is a good time to save your document. You can save it as a regular Word document (*.doc) and save it as a template later when all the work is finished.
The Next Step...
In the next step of the tutorial we build the VBA UserForm that will collect information from the user for insertion into the document.