• Error Message Help (2003)

    Author
    Topic
    #422842

    The following code is linked to a command button that starts a word document merge:

    Private Sub cmdMergeIt_Click()

    DoCmd.SetWarnings False
    DoCmd.OpenQuery “Approval”
    DoCmd.SetWarnings True

    Dim WordObj As Word.Document
    Dim strPathtoYourDocument As String

    strPathtoYourDocument = “C:Front_End_TrackingApproval_Letters” & DLookup(“DocumentName”, “Approval_Letters”, “DocumentID = ” & Me.[Vendor Combo Box].Column(1))

    Set WordObj = GetObject(strPathtoYourDocument)
    WordObj.Application.Visible = True
    WordObj.MailMerge.Destination = wdSendToNewDocument

    WordObj.MailMerge.Execute
    WordObj.Close wdDoNotSaveChanges

    Set WordObj = Nothing

    End Sub

    This works fine, except there are time where the data is incorrect or incomplete, and access throws up an unfriendly error message of “Word could not merge the main document with the data source because the data records were empty or no data records matched your query options.” Access then proceeds to bring up the approval document template, and spits out another error message “Run time error “5631′:…

    I’d like to replace the error messages with something a bit more user friendly like “The approval data is incomplete or incorrect” while at the same time, prevent Access from proceeding with the Word merge. I’ve played around with the “on error event” for the form, but I can’t get it to work.

    Thanks

    Viewing 0 reply threads
    Author
    Replies
    • #965661

      You can build error handling into the On Click procedure:

      Private Sub cmdMergeIt_Click()
      Dim WordObj As Word.Document
      Dim strPathtoYourDocument As String

      On Error GoTo ErrHandler

      DoCmd.SetWarnings False
      DoCmd.OpenQuery “Approval”
      DoCmd.SetWarnings True

      strPathtoYourDocument = “C:Front_End_TrackingApproval_Letters” & _
      DLookup(“DocumentName”, “Approval_Letters”, _
      “DocumentID = ” & Me.[Vendor Combo Box].Column(1))

      Set WordObj = GetObject(strPathtoYourDocument)
      WordObj.Application.Visible = True
      WordObj.MailMerge.Destination = wdSendToNewDocument
      WordObj.MailMerge.Execute

      ExitHandler:
      On Error Resume Next
      WordObj.Close wdDoNotSaveChanges
      Set WordObj = Nothing
      Exit Sub

      ErrHandler:
      MsgBox “Something went wrong.”, vbExclamation
      Resume ExitHandler
      End Sub

    Viewing 0 reply threads
    Reply To: Error Message Help (2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: