.GoTo and .TypeText - Stops When Field is Empty

alpinegroove

Registered User.
Local time
Today, 06:40
Joined
May 4, 2011
Messages
55
I am using VBA code to insert text from Access fields into bookmarked locations in Word.

However, the code aborts when it reaches an empty field. The document is created and data is inserted, but it does not insert data in bookmarks listed in the code below the empty field.

For example, if [StreetAddress] is empty, [State] and [ZipCode] will not be inserted even if those fields are not empty.

Is there a way to tell the code to continue even if a field is empty?

Thanks

Code:
Option Compare Database

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 = "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
    
   ' Replace each bookmark with field contents.
   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]
     
     If IsNull(Me.[CourseFinal D/T/R]) Then
     .GoTo what:=wdGoToBookmark, Name:="CourseFinalDTR"
     .TypeText Text:="N/A"
     Else
     .GoTo what:=wdGoToBookmark, Name:="CourseFinalDTR"
     .TypeText Me.[CourseFinal D/T/R]
     End If
              
     .GoTo what:=wdGoToBookmark, Name:="CourseFinalDate"
     .TypeText Me.[CourseFinal Date]
     
     .GoTo what:=wdGoToBookmark, Name:="CourseDisc1DT"
     .TypeText Me.[CourseDisc1D/T]
          
     .GoTo what:=wdGoToBookmark, Name:="CourseDisc1Rm"
     .TypeText Me.[CourseDisc1Rm]
     
     .GoTo what:=wdGoToBookmark, Name:="CourseDisc2DT"
     .TypeText Me.[CourseDisc2D/T]
          
     .GoTo what:=wdGoToBookmark, Name:="CourseDisc2Rm"
     .TypeText Me.[CourseDisc2Rm]
     
     .GoTo what:=wdGoToBookmark, Name:="CourseDisc3DT"
     .TypeText Me.[CourseDisc3D/T]
          
     .GoTo what:=wdGoToBookmark, Name:="CourseDisc3Rm"
     .TypeText Me.[CourseDisc3Rm]
     
     .GoTo what:=wdGoToBookmark, Name:="CourseDisc4DT"
     .TypeText Me.[CourseDisc4D/T]
          
     .GoTo what:=wdGoToBookmark, Name:="CourseDisc4Rm"
     .TypeText Me.[CourseDisc4Rm]
     
     .GoTo what:=wdGoToBookmark, Name:="InstructorComp"
     .TypeText FormatCurrency(Me.[Instructor])
     
     .GoTo what:=wdGoToBookmark, Name:="TAComp"
     .TypeText FormatCurrency(Me.[TA])
     
     .GoTo what:=wdGoToBookmark, Name:="ReaderComp"
     .TypeText FormatCurrency(Me.[Reader])
          
   End With
    
   DoEvents
   WordApp.Activate
    
   Set WordApp = Nothing
   Exit Sub

ErrHandler:
Set WordApp = Nothing

End Sub
 
You could use something like this on each:
Code:
If Len(Me.StreetAddress & vbNullString) > 0 Then
     .GoTo what:=wdGoToBookmark, Name:="StreetAddress"
     .TypeText Me.[StreetAddress]
End If
 
Thank you. What I did in the mean time, is insert "N/A" when the field was blank.
Thanks for the alternative.
 

Users who are viewing this thread

Back
Top Bottom