alpinegroove
Registered User.
- Local time
- Today, 05:26
- Joined
- May 4, 2011
- Messages
- 55
I am using VBA code to merge data from Access into a Word document.
My code currently opens a Word template and inserts the data into it.
However, is it possible to designate 3 different templates and include an IF clause that determines which template will be used?
I have a field that indicates whether a person is an Instructor, TA, or Reader for a given course. Each position requires a different type of contract, so I would like the VBA code to look at that field and based on what's in the field, choose the appropriate template.
Is that possible?
The code I currently use for a 1 template situation is below.
Thank you.
My code currently opens a Word template and inserts the data into it.
However, is it possible to designate 3 different templates and include an IF clause that determines which template will be used?
I have a field that indicates whether a person is an Instructor, TA, or Reader for a given course. Each position requires a different type of contract, so I would like the VBA code to look at that field and based on what's in the field, choose the appropriate template.
Is that possible?
The code I currently use for a 1 template situation is below.
Thank you.
Code:
Private Sub GenerateContract_Click()
' Check for empty fields and unsaved record.
If IsNull(FirstName) Then
MsgBox "First name cannot be empty"
Me.FirstName.SetFocus
Exit Sub
End If
If IsNull(LastName) Then
MsgBox "Last name cannot be empty"
Me.LastName.SetFocus
Exit Sub
End If
If IsNull(StreetAddress) Then
MsgBox "Street address cannot be empty"
Me.StreetAddress.SetFocus
Exit Sub
End If
If Me.Dirty Then
If MsgBox("Record has not been saved. " & Chr(13) & _
"Do you want to save it?", vbInformation + vbOKCancel) = vbOK Then
DoCmd.RunCommand acCmdSaveRecord
Else
Exit Sub
End If
End If
' Create a Word document from template.
Dim WordApp As Word.Application
Dim strTemplateLocation As String
' Specify location of template
strTemplateLocation = "C:\Instructor Contract Template.doc"
On Error Resume Next
Set WordApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set WordApp = CreateObject("Word.Application")
End If
On Error GoTo ErrHandler
WordApp.Visible = True
WordApp.WindowState = wdWindowStateMaximize
WordApp.Documents.Add Template:=strTemplateLocation, NewTemplate:=False
' Replace each bookmark with field contents.
With WordApp.Selection
.Goto what:=wdGoToBookmark, Name:="Name"
.TypeText Trim([FirstName] & " " & [LastName])
.Goto what:=wdGoToBookmark, Name:="StreetAddress"
.TypeText [StreetAddress]
.Goto what:=wdGoToBookmark, Name:="City"
.TypeText [City]
.Goto what:=wdGoToBookmark, Name:="State"
.TypeText [State]
.Goto what:=wdGoToBookmark, Name:="ZipCode"
.TypeText [ZipCode]
.Goto what:=wdGoToBookmark, Name:="Name2"
.TypeText Trim([FirstName] & " " & [LastName])
' These fields are currency in Access:'
.Goto what:=wdGoToBookmark, Name:="InstructorComp"
.TypeText [Instructor]
.Goto what:=wdGoToBookmark, Name:="TAComp"
.TypeText [TA]
.Goto what:=wdGoToBookmark, Name:="ReaderComp"
.TypeText [Reader]
End With
DoEvents
WordApp.Activate
Set WordApp = Nothing
Exit Sub
ErrHandler:
Set WordApp = Nothing
End Sub