Access.2007.VBA.Bibl..

(John Hannent) #1
The cmdWord_Click()event procedure first saves information to variables, for use later in the
code, then checks that the template is found in the templates folder, sets a Word Application vari-
able, and creates a new Word document based on the template. Next, it sets a reference to the
Word CustomDocumentProperties collection of the newly created document and sets each docu-
ment property to either a variable or the value in a field from the current record. The segment of
code that creates a save name for the document (used in most of my export procedures) uses a
Do...Loopstatement to create a save name for the document containing the merged data, pick-
ing up the contact name from a field on the form, adding today’s date, in a format that uses dashes
to create an acceptable file name:

Private Sub cmdWord_Click()

On Error GoTo ErrorHandler

Dim appWord As Word.Application
Dim strCompanyName As String
Dim strContactName As String
Dim strWholeAddress As String
Dim strJobTitle As String
Dim docs As Word.Documents
Dim doc As Word.Document
Dim strWordTemplate As String
Dim strDocsPath As String
Dim strTemplatePath As String
Dim prps As Object
Dim strShortDate As String
Dim strLongDate As String
Dim strTest As String
Dim strAddress As String
Dim strCountry As String
Dim strSaveName As String
Dim strTestFile As String
Dim intSaveNameFail As Boolean
Dim i As Integer
Dim strSaveNamePath As String

Check for required address information:

strTest = Nz(Me![StreetAddress])
If strTest = “” Then
MsgBox “Can’t send letter -- no address!”
GoTo ErrorHandlerExit
End If

strContactName = _
Nz(Me![ContactName])
strCompanyName = _
Nz(Me![CompanyName])
strWordTemplate = “Contact Letter Doc Props.dotx”

Part II Writing VBA Code to Exchange Data between Office Components

Free download pdf