Code:
Option Compare Database
Private Sub GenerateContract_Click()
' Create a Word document from template.
Dim WordApp As Word.Application
Dim strTemplateLocation As String
If (IsNull(Me.[Instructor]) = False) Then
' Specify location of template
strTemplateLocation = "H:\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
' Finds Bookmark in the template listed above.
' Bookmark name from Word is listed under Name.
' Inserts field contents into bookmarked location.
' If a field is empty, the code will stop at that field.
With WordApp.Selection
.GoTo what:=wdGoToBookmark, Name:="Name"
.TypeText Trim(Me.[FirstName] & " " & Me.[LastName])
.GoTo what:=wdGoToBookmark, Name:="StreetAddress"
.TypeText Me.[StreetAddress]
.GoTo what:=wdGoToBookmark, Name:="City"
.TypeText Me.[City]
.GoTo what:=wdGoToBookmark, Name:="State"
.TypeText Me.[State]
.GoTo what:=wdGoToBookmark, Name:="ZipCode"
.TypeText Me.[ZipCode]
.GoTo what:=wdGoToBookmark, Name:="Name2"
.TypeText Trim(Me.[FirstName] & " " & Me.[LastName])
.GoTo what:=wdGoToBookmark, Name:="CourseTitle"
.TypeText Me.[CourseTitle]
.GoTo what:=wdGoToBookmark, Name:="Section"
.TypeText Me.[SectionNumber]
.GoTo what:=wdGoToBookmark, Name:="CourseDT"
.TypeText Me.[CourseD/T]
.GoTo what:=wdGoToBookmark, Name:="CourseRoom"
.TypeText Me.[CourseRoom]
' Not all courses have a final exam.
' Since the code stops if a field is null, this line inserts N/A if the
' field is empty and the code continues.
' Nz means that if the field has data in it, the data will be inserted.
' If the field is empty, N/A will be inserted.
.GoTo what:=wdGoToBookmark, Name:="CourseFinalDTR"
.TypeText Nz(Me.[CourseFinalD/T/R], "N/A")
.GoTo what:=wdGoToBookmark, Name:="CourseFinalDate"
.TypeText Nz(Me.[CourseFinalDate], "N/A")
.GoTo what:=wdGoToBookmark, Name:="CourseDisc1DT"
.TypeText Nz(Me.[CourseDisc1D/T], "N/A")
.GoTo what:=wdGoToBookmark, Name:="CourseDisc1Rm"
.TypeText Nz(Me.[CourseDisc1Rm], "N/A")
.GoTo what:=wdGoToBookmark, Name:="CourseDisc2DT"
.TypeText Nz(Me.[CourseDisc2D/T], "N/A")
.GoTo what:=wdGoToBookmark, Name:="CourseDisc2Rm"
.TypeText Nz(Me.[CourseDisc2Rm], "N/A")
.GoTo what:=wdGoToBookmark, Name:="CourseDisc3DT"
.TypeText Nz(Me.[CourseDisc3D/T], "N/A")
.GoTo what:=wdGoToBookmark, Name:="CourseDisc3Rm"
.TypeText Nz(Me.[CourseDisc3Rm], "N/A")
.GoTo what:=wdGoToBookmark, Name:="CourseDisc4DT"
.TypeText Nz(Me.[CourseDisc4D/T], "N/A")
.GoTo what:=wdGoToBookmark, Name:="CourseDisc4Rm"
.TypeText Nz(Me.[CourseDisc4Rm], "N/A")
' This data needs to remain with its currency formatting.
' Nz cannot handle the formatting code.
' Instead, this IIf statement inserts N/A if the field is null
' and field contents with currency formatting if it is not null.
.GoTo what:=wdGoToBookmark, Name:="InstructorComp"
.TypeText IIf(IsNull(Me.[Instructor]), "N/A", FormatCurrency(Me.[Instructor]))
.GoTo what:=wdGoToBookmark, Name:="TAComp"
.TypeText IIf(IsNull(Me.[TA]), "N/A", FormatCurrency(Me.[TA]))
.GoTo what:=wdGoToBookmark, Name:="ReaderComp"
.TypeText IIf(IsNull(Me.[Reader]), "N/A", FormatCurrency(Me.[Reader]))
End With
DoEvents
WordApp.Activate
Set WordApp = Nothing
Exit Sub
End If
If IsNull(Me.[Instructor]) And (IsNull(Me.[TA]) = False) And IsNull(Me.[Reader]) Then
' Specify location of template
strTemplateLocation = "H:\TA 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
' Finds Bookmark in the template listed above.
' Bookmark name from Word is listed under Name.
' Inserts field contents into bookmarked location.
' If a field is empty, the code will stop at that field.
With WordApp.Selection
.GoTo what:=wdGoToBookmark, Name:="Name"
.TypeText Trim(Me.[FirstName] & " " & Me.[LastName])
.GoTo what:=wdGoToBookmark, Name:="StreetAddress"
.TypeText Me.[StreetAddress]
.GoTo what:=wdGoToBookmark, Name:="City"
.TypeText Me.[City]
.GoTo what:=wdGoToBookmark, Name:="State"
.TypeText Me.[State]
.GoTo what:=wdGoToBookmark, Name:="ZipCode"
.TypeText Me.[ZipCode]
.GoTo what:=wdGoToBookmark, Name:="Name2"
.TypeText Trim(Me.[FirstName] & " " & Me.[LastName])
.GoTo what:=wdGoToBookmark, Name:="CourseTitle"
.TypeText Me.[CourseTitle]
.GoTo what:=wdGoToBookmark, Name:="Section"
.TypeText Me.[SectionNumber]
' Not all courses have a final exam or discussion sections.
' Since the code stops if a field is null, this line inserts N/A if the
' field is empty and the code continues.
' Nz means that if the field has data in it, the data will be inserted.
' If the field is empty, N/A will be inserted.
.GoTo what:=wdGoToBookmark, Name:="CourseFinalDTR"
.TypeText Nz(Me.[CourseFinalD/T/R], "N/A")
.GoTo what:=wdGoToBookmark, Name:="CourseFinalDate"
.TypeText Nz(Me.[CourseFinalDate], "N/A")
.GoTo what:=wdGoToBookmark, Name:="CourseDisc1DT"
.TypeText Nz(Me.[CourseDisc1D/T], "N/A")
.GoTo what:=wdGoToBookmark, Name:="CourseDisc1Rm"
.TypeText Nz(Me.[CourseDisc1Rm], "N/A")
.GoTo what:=wdGoToBookmark, Name:="CourseDisc2DT"
.TypeText Nz(Me.[CourseDisc2D/T], "N/A")
.GoTo what:=wdGoToBookmark, Name:="CourseDisc2Rm"
.TypeText Nz(Me.[CourseDisc2Rm], "N/A")
.GoTo what:=wdGoToBookmark, Name:="CourseDisc3DT"
.TypeText Nz(Me.[CourseDisc3D/T], "N/A")
.GoTo what:=wdGoToBookmark, Name:="CourseDisc3Rm"
.TypeText Nz(Me.[CourseDisc3Rm], "N/A")
.GoTo what:=wdGoToBookmark, Name:="CourseDisc4DT"
.TypeText Nz(Me.[CourseDisc4D/T], "N/A")
.GoTo what:=wdGoToBookmark, Name:="CourseDisc4Rm"
.TypeText Nz(Me.[CourseDisc4Rm], "N/A")
.GoTo what:=wdGoToBookmark, Name:="TAComp"
.TypeText FormatCurrency(Me.[TA])
End With
DoEvents
WordApp.Activate
Set WordApp = Nothing
Exit Sub
End If
If IsNull(Me.[Instructor]) And IsNull(Me.[TA]) And (IsNull(Me.[Reader]) = False) Then
' Specify location of template
strTemplateLocation = "H:\Reader 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
' Finds Bookmark in the template listed above.
' Bookmark name from Word is listed under Name.
' Inserts field contents into bookmarked location.
' If a field is empty, the code will stop at that field.
With WordApp.Selection
.GoTo what:=wdGoToBookmark, Name:="Name"
.TypeText Trim(Me.[FirstName] & " " & Me.[LastName])
.GoTo what:=wdGoToBookmark, Name:="StreetAddress"
.TypeText Me.[StreetAddress]
.GoTo what:=wdGoToBookmark, Name:="City"
.TypeText Me.[City]
.GoTo what:=wdGoToBookmark, Name:="State"
.TypeText Me.[State]
.GoTo what:=wdGoToBookmark, Name:="ZipCode"
.TypeText Me.[ZipCode]
.GoTo what:=wdGoToBookmark, Name:="Name2"
.TypeText Trim(Me.[FirstName] & " " & Me.[LastName])
.GoTo what:=wdGoToBookmark, Name:="CourseTitle"
.TypeText Me.[CourseTitle]
.GoTo what:=wdGoToBookmark, Name:="Section"
.TypeText Me.[SectionNumber]
' Not all courses have a final exam or discussion sections.
' Since the code stops if a field is null, this line inserts N/A if the
' field is empty and the code continues.
' Nz means that if the field has data in it, the data will be inserted.
' If the field is empty, N/A will be inserted.
.GoTo what:=wdGoToBookmark, Name:="CourseFinalDTR"
.TypeText Nz(Me.[CourseFinalD/T/R], "N/A")
.GoTo what:=wdGoToBookmark, Name:="CourseFinalDate"
.TypeText Nz(Me.[CourseFinalDate], "N/A")
.GoTo what:=wdGoToBookmark, Name:="ReaderComp"
.TypeText FormatCurrency(Me.[Reader])
End With
DoEvents
WordApp.Activate
Set WordApp = Nothing
Exit Sub
End If
ErrHandler:
Set WordApp = Nothing
End Sub