Link Ms access with word file

HiThis question is related to:
"How to save mail merge in ms access 2007 with a specific name in word
12/20/11 03:15 PM, ID: 37317775"
new question is:
if some field in the temp1 table is empty I am getting an error.

This the code written by Helen_Feddema:
Public Sub CreateWordDocsAndEmailAsPDFs()
'Created by Helen Feddema 20-Dec-2011
'Last modified by Helen Feddema 28-Dec-2011

On Error GoTo ErrorHandler

   Dim appOutlook As New Outlook.Application
   Dim appWord As New Word.Application
   Dim strTemplatesPath As String
   Dim strTemplate As String
   Dim doc As Word.Document
   Dim rst As DAO.Recordset
   Dim prps As Object
   Dim strDocsPath As String
   Dim strSaveName As String
   Dim strShortDate As String
   Dim msg As Outlook.MailItem
   strShortDate = Format(Date, "m-ddd-yyyy")
   Set rst = CurrentDb.OpenRecordset("temp1")
   strDocsPath = appWord.Options.DefaultFilePath(wdDocumentsPath) & "\"
   strTemplatesPath = appWord.Options.DefaultFilePath(wdUserTemplatesPath) & "\"
   strTemplate = strTemplatesPath & ""
   appWord.Visible = True
   Do While Not rst.EOF
      Set doc = appWord.Documents.Add(strTemplate)
      'Fill Word doc properties with data from variables
      Set prps = doc.CustomDocumentProperties
      prps.Item("No").Value = rst![NA1]
      prps.Item("Customer").Value = rst![CUST]
      prps.Item("Amount").Value = rst![AMOUNT]
      'Update fields in Word document
      With appWord.Selection
         .HomeKey Unit:=wdStory
      End With
      strSaveName = strDocsPath & "Tay document for " _
         & rst![CUST] & ", No. " & rst![NA1] & " on " _
         & strShortDate & ".pdf"
      Debug.Print strSaveName
      'doc.SaveAs FileName:=strSaveName, FileFormat:=wdFormatDocument
      doc.ExportAsFixedFormat OutputFileName:=strSaveName, _
         ExportFormat:=wdExportFormatPDF, _
      doc.Close savechanges:=wdDoNotSaveChanges
      Set msg = appOutlook.CreateItem(olMailItem)
      msg.Subject = "Tay document for " & rst![CUST] _
         & ", No. " & rst![NA1]
      msg.To = "Email name"
      msg.Categories = "Tay document"
      msg.Attachments.Add strSaveName
      'Use this line to send immediately
   Set appWord = Nothing
   Set appOutlook = Nothing
   Exit Sub

   MsgBox "Error No: " & Err.Number _
      & " in CreateWordDocsAndEmailAsPDFs procedure; " _
      & "Description: " & Err.Description
   Resume ErrorHandlerExit

End Sub
Who is Participating?
SheilsConnect With a Mentor Commented:
Use the Nz function eg:

prps.Item("No").Value = Nz(rst![NA1]," ")

This function returns an alternate value when there is an error.
HKH1967Author Commented:
sb9: thanks
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.