Microsoft Access 2010 Bible

(Rick Simeone) #1

Part III: More-Advanced Access Techniques


806


Creating a new document based


on an existing template
After Word is running, a blank document is created. The following code creates a new document
by using the Thanks.dotx template:

WordObj.Documents.Add _
Template:=“C:\Thanks.dotx”, NewTemplate:=False

Note
The path must be set correctly in order to find the Thanks.dotx template on your computer.


The Thanks.dotx template contains bookmarks (refer to Figure 22.6) indicating where to insert
data into the new Word document. To create a bookmark in Word:


  1. Highlight the text that you want to make a bookmark.

  2. In the Link group on Word’s Insert ribbon, select the Bookmark command.

  3. Enter the bookmark name.

  4. Click Add.


Inserting data
The code uses the Selection object’s GoTo method to move to bookmarks in the Word docu-
ment. After moving to the bookmark, the text contained within the bookmark is selected. By
inserting text (with Automation or by manually typing directly into the document), the code
replaces the bookmark text. Use the TypeText method of the Selection object to insert text:

WordObj.Selection.Goto what:=wdGoToBookmark, Name:=“FullName”
WordObj.Selection.TypeText rsCust![ContactName]

Note
You can’t pass Null to the TypeText method. If the value may possibly be Null, you need to check ahead
and make allowances. The preceding sample code checks the Address2 field for a Null value and acts
accordingly. If you don’t pass text to replace the bookmark — even just a zero-length string (““) — the book-
mark text remains in the document. If you’re okay with putting a zero-length string into the letter, you can
always use the Nz function.


Activating the instance of Word
To enable the user to enter data in the new document, you must activate the Word application. If
you don’t make Word the active application, the user has to manually switch to Word from Access.
Activate Word with the following statement:

WordObj.Activate
Free download pdf