Results 1 to 3 of 3
  1. #1
    Star Lounger
    Join Date
    Mar 2002
    Location
    Chicago, Illinois, USA
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Error Message Help (2003)

    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

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Error Message Help (2003)

    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.<!t>[Vendor Combo Box]<!/t>.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

  3. #3
    Star Lounger
    Join Date
    Mar 2002
    Location
    Chicago, Illinois, USA
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Error Message Help (2003)

    I'll give it a shot. Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •