Results 1 to 4 of 4
  1. #1
    tallen is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2019
    Posts
    8

    Access Query merge into Word is not working in Access 2016

    I am not a programmer. With this said, our database was recently upgraded to 2016. I am getting errors on one of our forms which uses a query to merge to one of any 10 word merge documents. The two errors I am receiving are error 91 on the With oWdoc.MailMergee line and the "expect a name error on the mailmerge section. Can anyone help me. I have spent three days researching this and found a thread on this forum similar, but do not know how to apply it to my specific form. I am thinking I am missing a line or similar. I have included all code associated with this form in this post. The form is in Access 2016. I am disheartened by this not being able to get the merge to work. Within my database, I am using an Access Query which is used to fill out several word merge documents. The query is called "mqpinsp". Have I done something as simple as left out a bracket or quotations?



    The errors occur once I call on the mail merge. An error 91 occurs and an error indicating a name is expected occurs. Both errors are in the following section of the code,

    Code:
    With oWdoc.MailMerge
            .MainDocumentType = wdFormLetters
            .OpenDataSource _
                Name:=CurrentProject.FullName, _
                ReadOnly:=True, _
                AddToRecentFiles:=False, _
                LinkToSource:=True, _
                Connection:="QUERY mqpinsp",
                SQLStatement:="SELECT * FROM [mqpinsp]        ' Change the table name or your query"
            .Destination = wdSendToNewDocument
            .Execute Pause:=False
        End With

    The only code on this one form is this:

    Code:
    Option Compare DatabaseOption Explicit
    
    
    Private Sub cmdQUIT_Click()
    DoCmd.Close acForm, "fLettersPInsp"
    End Sub
    
    
    Private Sub lstLetters_Click()
    Dim strWordDoc  As String
    
    
        'Path to the word document of the Mail Merge
        '###-1 CHANGE THE FOLLOWING LINE TO POINT TO YOUR DOCUMENT!!
        strWordDoc = "H:\USERS\Templates" & "\" & "lstLetters.Column(0)"
    
    
    Dim MergeQuery As String
    MergeQuery = lstLetters.Column(1)
    
    
        ' Call the code to merge the latest info
        startMerge strWordDoc
    
    
    End Sub
    
    
    
    
    '----------------------------------------------------
    ' Auto Mail Merge With VBA and Access (Early Binding)
    '----------------------------------------------------
    ' NOTE: To use this code, you must reference
    ' The Microsoft Word 14.0 (or current version)
    ' Object Library by clicking menu Tools > References
    ' Check the box for:
    ' Microsoft Word 14.0 Object Library in Word 2010
    ' Microsoft Word 15.0 Object Library in Word 2013
    ' Click OK
    '----------------------------------------------------
    Function startMerge(strDocPath As String)
        Dim oWord           As Word.Application
        Dim oWdoc           As Word.Document
        Dim wdInputName     As String
        Dim wdOutputName    As String
        Dim outFileName     As String
    
    
        ' Set Template Path
        wdInputName = strDocPath            ' was CurrentProject.Path & "\mail_merge.docx"
    
    
        ' Create unique save filename with minutes and seconds to prevent overwrite
        outFileName = "MailMergeFile_" & Format(Now(), "yyyymmddmms")
    
    
        ' Output File Path w/outFileName
        wdOutputName = CurrentProject.Path & "\" & outFileName
    
    
        Set oWord = New Word.Application
        Set oWdoc = oWord.Documents.Open(wdInputName)
    
    
        ' Start mail merge
    
    
        With oWdoc.MailMerge
            .MainDocumentType = wdFormLetters
            .OpenDataSource _
                Name:=CurrentProject.FullName, _
                ReadOnly:=True, _
                AddToRecentFiles:=False, _
                LinkToSource:=True, _
                Connection:="QUERY mqpinsp",
                SQLStatement:="SELECT * FROM [mqpinsp]        ' Change the table name or your query"
            .Destination = wdSendToNewDocument
            .Execute Pause:=False
        End With
    
    
        ' Hide Word During Merge
        oWord.Visible = False
    
    
        ' Save file as PDF
        ' Uncomment the line below and comment out
        ' the line below "Save file as Word Document"
        '------------------------------------------------
        'oWord.ActiveDocument.SaveAs2 wdOutputName & ".pdf", 17
    
    
        ' Save file as Word Document
        ' ###-3 IF YOU DON'T WANT TO SAVE AS A NEW NAME, COMMENT OUT NEXT LINE
        oWord.ActiveDocument.SaveAs2 wdOutputName & ".docx", 16
    
    
        ' SHOW THE DOCUMENT
        oWord.Visible = True
    
    
        ' Close the template file
        If oWord.Documents(1).FullName = strDocPath Then
            oWord.Documents(1).Close savechanges:=False
        ElseIf oWord.Documents(2).FullName = strDocPath Then
            oWord.Documents(2).Close savechanges:=False
        Else
            MsgBox "Well, this should never happen! Only expected two documents to be open"
        End If
    
    
        ' Quit Word to Save Memory
        'oWord.Quit savechanges:=False
    End Function

  2. #2
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    Not sure if its a copy and paste error but Option Explicit should be on its own line.

    also - SQLStatement:="SELECT * FROM [mqpinsp] ' Change the table name or your query" is missing the closing quote after [mqpinsp].

  3. #3
    tallen is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2019
    Posts
    8
    Thank you. I made the correction, but now I went back to receiving a Compile error for Syntax on the Open.Document command line area. Could this be because the Access 2016 database has VBA programming vs. SQL?

    The subform has 5 Word Merge documents. The user picks one of these Word Docs and the Code should tell the database to use the query [mqpinsp] to merge the document clicked on for the current facility record selected.


    Code:
    Option Compare Database
    Option Explicit
    
    
    Private Sub cmdQUIT_Click()
    DoCmd.Close acForm, "fLettersPInsp"
    End Sub
    
    
    Private Sub lstLetters_Click()
    Dim strWordDoc  As String
    
    
        'Path to the word document of the Mail Merge
        '###-1 CHANGE THE FOLLOWING LINE TO POINT TO YOUR DOCUMENT!!
        strWordDoc = "H:\USERS\Templates" & "\" & "lstLetters.Column(0)"
    
    
    Dim MergeQuery As String
    MergeQuery = lstLetters.Column(1)
    
    
        ' Call the code to merge the latest info
        startMerge strWordDoc
    
    
    End Sub
    
    
    
    
    '----------------------------------------------------
    ' Auto Mail Merge With VBA and Access (Early Binding)
    '----------------------------------------------------
    ' NOTE: To use this code, you must reference
    ' The Microsoft Word 14.0 (or current version)
    ' Object Library by clicking menu Tools > References
    ' Check the box for:
    ' Microsoft Word 14.0 Object Library in Word 2010
    ' Microsoft Word 15.0 Object Library in Word 2013
    ' Click OK
    '----------------------------------------------------
    Function startMerge(strDocPath As String)
        Dim oWord           As Word.Application
        Dim oWdoc           As Word.Document
        Dim wdInputName     As String
        Dim wdOutputName    As String
        Dim outFileName     As String
    
    
        ' Set Template Path
        wdInputName = strDocPath            ' was CurrentProject.Path & "\mail_merge.docx"
    
    
        ' Create unique save filename with minutes and seconds to prevent overwrite
        outFileName = "MailMergeFile_" & Format(Now(), "yyyymmddmms")
    
    
        ' Output File Path w/outFileName
        wdOutputName = CurrentProject.Path & "\" & outFileName
    
    
        Set oWord = New Word.Application
        Set oWdoc = oWord.Documents.Open(wdInputName)
    
    
        ' Start mail merge
    
    
        '###-2 CHANGE THE SQLSTATEMENT AS NEEDED
        With oWdoc.MailMerge
            .MainDocumentType = wdFormLetters
            .OpenDataSource _
                Name:=CurrentProject.strWordDoc, _
                ReadOnly:=True, _
                AddToRecentFiles:=False, _
                LinkToSource:=True, _
                Connection:="QUERY mqpinsp",
                SQLStatement:="SELECT * FROM [mqpinsp]"        ' Change the table name or your query"
            .Destination = wdSendToNewDocument
            .Execute Pause:=False
        End With
    
    
        ' Hide Word During Merge
        oWord.Visible = False
    
    
        ' Save file as PDF
        ' Uncomment the line below and comment out
        ' the line below "Save file as Word Document"
        '------------------------------------------------
        'oWord.ActiveDocument.SaveAs2 wdOutputName & ".pdf", 17
    
    
        ' Save file as Word Document
        ' ###-3 IF YOU DON'T WANT TO SAVE AS A NEW NAME, COMMENT OUT NEXT LINE
        oWord.ActiveDocument.SaveAs2 wdOutputName & ".docx", 16
    
    
        ' SHOW THE DOCUMENT
        oWord.Visible = True
    
    
        ' Close the template file
        If oWord.Documents(1).FullName = strDocPath Then
            oWord.Documents(1).Close savechanges:=False
        ElseIf oWord.Documents(2).FullName = strDocPath Then
            oWord.Documents(2).Close savechanges:=False
        Else
            MsgBox "Well, this should never happen! Only expected two documents to be open"
        End If
    
    
        ' Quit Word to Save Memory
        'oWord.Quit savechanges:=False
    End Function

  4. #4
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    Code:
    strWordDoc = "H:\USERS\Templates" & "\" & "lstLetters.Column(0)"
    this line looks wrong and should probably be something like
    Code:
    strWordDoc = "H:\USERS\Templates\"  & Me.lstLetters.Column(0)
    You should add a debug line right after the above line
    Code:
    Debug.Print strWordDoc
    This way you can be sure of what it resolves to and is a valid path.

    To be honest I'm not overly knowledgable with Word Vba. I always use Albert Kallal's Super Easy Word Merge, as it is easily integrated in any Db and is easily modified.

    You can google Alberts code. Its everywhere.
    I recently posted an example of what I use in this thread Post #7 ...https://www.accessforums.net/showthr...882#post431882

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 05-01-2018, 12:16 PM
  2. Replies: 3
    Last Post: 02-28-2018, 03:18 AM
  3. Replies: 6
    Last Post: 08-13-2014, 10:32 PM
  4. Access Query to Word Merge
    By besuchanko in forum Import/Export Data
    Replies: 1
    Last Post: 02-24-2013, 05:17 PM
  5. Access Query mail merge to Word
    By Jan Collier in forum Access
    Replies: 8
    Last Post: 08-30-2010, 09:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums