VBA debug message

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lchomany
    New Member
    • Sep 2007
    • 3

    #1

    VBA debug message

    Can anyone tell me what the compiled error would be here?
    Saying "User-defined type not found" and
    highlighted

    objWord As Word.Document in yellow
    _______________ _______________

    Code:
    Private Sub cmdWordMailMerge_Click()
    On Error GoTo Err_cmdWordMailMerge_Click
    
        Dim strMsg, strMsgStyle, strMsgResponse, strPath, strName As String
        Dim objWord As Word.Document
        Dim objFileSystem As FileSystemObject
        
        Set objFileSystem = New FileSystemObject
        
        If IsNull(Me.cboDocumentDesc) Then
            strMsg = "A Document must be specified before a Document" _
                    & " Mail Merge can be run!"
            Err.Raise 9999
        End If
        
        If Me.cboDocumentDesc.Column(5) = "" Then
            strMsg = "A Word Document Path/File Name must be defined in the Document" _
                    & " Definition before a mail merge can be run!"
            Err.Raise 9999
        End If
        
        If objFileSystem.FileExists(Me.cboDocumentDesc.Column(5)) = False Then
            strMsg = "The Word Document Path/File Name is invalid. This must be corrected" _
                    & " in the Document Definition before Email Distribution can be run!"
            Err.Raise 9999
        End If
        
        'set the path to the word document
        strPath = """" & Me.cboDocumentDesc.Column(5) & """"
        Set objWord = GetObject(strPath, "Word.Document")
        
        ' make Word visible
        objWord.Application.Visible = True
        
        'set datasource
        objWord.MailMerge.OpenDataSource _
            Name:="""" & CurrentProject.FullName & """", _
            LinkToSource:=True, _
            Connection:="TABLE tblDocumentDistributionList", _
            SQLStatement:="Select Addressee, AddressLine1, AddressLine2, AddressLine3," _
                        & " AddressLine4, AddressLine5, Salutation FROM [tblDocumentDistributionList]" _
                        & " where DocumentType = '" & Me.txtDocumentType & "'" _
                        & " order by EntityName"
            
        'execute mail merge
        objWord.MailMerge.Destination = wdSendToNewDocument
        objWord.MailMerge.Execute
        
        'remove datasource and query options - close and discard the object
        objWord.MailMerge.MainDocumentType = wdNotAMergeDocument
        objWord.Close SaveChanges:=False
        Set objWord = Nothing
        
    Exit_cmdWordMailMerge_Click:
        Exit Sub
    
    Err_cmdWordMailMerge_Click:
        'set message and display message box
        Select Case Err.Number
            Case 287   'document is open
                strMsg = "The selected Word Document is open. Please ensure that the" _
                    & " document is closed and try the merge again."
                strMsgStyle = vbExclamation
                strMsgResponse = MsgBox(strMsg, strMsgStyle)
            Case 432   'file name/path invalid
                strMsg = "The Word Document Path/File Name is invalid. Please verify and" _
                    & " correct the Document Definition record."
                strMsgStyle = vbExclamation
                strMsgResponse = MsgBox(strMsg, strMsgStyle)
            Case 9999   'application or object defined error
                strMsgStyle = vbExclamation
                strMsgResponse = MsgBox(strMsg, strMsgStyle)
            Case Else   'all other errors
                strMsg = "Error " & Err.Number & " " & Err.Description
                strMsgStyle = vbCritical
                strMsgResponse = MsgBox(strMsg, strMsgStyle)
        End Select
        Resume Exit_cmdWordMailMerge_Click
        
    End Sub
    This was prepared in Access 98 (they think). Trying to send a document to mail merge.
    Last edited by NeoPa; Oct 14 '08, 07:48 PM. Reason: Please remember to use the [CODE] tags provided
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32669

    #2
    I assume you're referring to line #5, and that the Yellow highlights the Dim part as well.

    This will happen if the Word reference is not set up in the project.

    From the VBA Editor window select Tools / References...

    From the list select the relevant Word library.

    Compile project again.

    Comment

    Working...