Creating a Word Document Based on a Template

The functions in this section (and later sections) pick up the Contact Templates folder path, and the Contact Documents folder path, from the main menu. Each path has a command button and a TextBox bound to a field in tblInfo; clicking the command button opens a FolderPicker dialog for selecting the path. The selected path is displayed in the TextBox, as shown in Figure 6.6.

FIGURE 6.6

The main menu of the Word Export sample database.

Word Export !

I ContactTemplatea Patri... j jD:\Templates\Contact Templates [contact Documents pnth...| D:\Documents\Contact Letters

I ContactTemplatea Patri... j jD:\Templates\Contact Templates [contact Documents pnth...| D:\Documents\Contact Letters

If you click the Contact Templates Path or Contact Documents Path button, a Browse dialog opens, where you can select the folder for storing templates or documents (see Figure 6.7).

i r, rj -Vf-v. v., r.V The procedures that pop up these Browse dialogs are discussed in Chapter 9.

FIGURE 6.7

Selecting a folder as the Contact Documents folder.

Browse for folder where coirtact documents should he s<wecl

% Organize * Views * New Folder

Favorite Links k Computer S Desktop Qfe Documents Ijj Network Shortcuts [J Recent k Computer S| Desktop ¡j| Recent Places [¿i Contact Letters

Contact Templates ^ Office Book

Folders

Name Date modified 1

I ¿) Access 2000 databases L|J Access 2002-2003 Databases I ¿i Access 2007 Databases ¿i Access Meige |J Adobe /1 Attachments l/j Backups ! fj BBC Project I ¿j Beta ! Booklets [¿.i Bugs

I Cards, Labels & Envelopes I ¿j Computer [¿i Contact OfiL-1 C ontnct Letters I ¿j Contacts I ¿j Correspondence

Folder name: Contact Letters

The NewDocFromTemplate function listed next creates a new document based on a template in the Contact Templates folder, using Word document properties to hold the data from Access. The document properties method is the most common technique I use for creating documents to fill with Access data. In the NewDocFromTemplate procedure, after creating the new document, the names of its document properties are printed to the Immediate window.

MOT.

As with previous versions of Word, although there is a CustomProperties collection in the Word 2007 object model, and a CustomProperty object, this collection and object actually belong to the Smart Tags, so if you declare variables of these data types, you will get a compile error; therefore, they must be declared as Object.

Public Function NewDocFromTemplate()

On Error GoTo ErrorHandler

Dim appWord As Word.Application Dim docs As Word.Documents Dim strLetter As String Dim strTemplateDir As String Dim doc As Word.Document

Must declare this variable as Object because declaring it as Word.CustomProperties doesn't work: Dim prps As Object

Must declare this variable as Object because declaring it as Word.CustomProperty doesn't work: Dim prp As Object

Set appWord = GetObject(Class:="Word.Application")

strTemplateDir = GetContactsTemplatesPath() Debug.Print "Templates directory: " & strTemplateDir strLetter = strTemplateDir & "DocProps.dot" Debug.Print "Letter: " & strLetter

Set docs = appWord.Documents docs.Add strLetter

Set doc = appWord.ActiveDocument

Set prps = doc.CustomDocumentProperties

For Each prp In prps

Debug.Print "Property name: " & prp.Name Next prp

ErrorHandlerExit: Exit Function

ErrorHandler:

If Err = 429 Then

'Word is not running; open Word with CreateObject Set appWord = CreateObject(Class:="Word.Application") Resume Next Else

& "; Description: " & Err.Description Resume ErrorHandlerExit End If

End Function

0 0

Post a comment