none
BUG: Mail Merge with Access and Word. RRS feed

  • General discussion

  • I spent most of Friday trying to get Access to automate Word to run a simple mail merge, without success.
    I have used the following process:
    '===========================================================================
    Public Function ViewOrPrintMergedDoc(strFile As String, bPreview As Boolean) As Boolean
    Dim appWord As Object
    Dim wdocSource As Object
    Dim strSQL As String
    
    On Error Resume Next
       Set appWord = GetObject(, "Word.Application")
    On Error GoTo HandleErr
    If appWord Is Nothing Then
       Set appWord = CreateObject("Word.Application")
    End If
    
    strSQL = "SELECT PrintQuery2.* from PrintQuery2"
    
    Set wdocSource = appWord.Documents.Open(strFile & ".docx")
    With wdocSource
      .MailMerge.MainDocumentType = 0 'wdFormLetters=0
      .MailMerge.Destination = 0 'wdSendToNewDocument
      .MailMerge.OpenDataSource Name:="TempData.accdb", _ 
      SQLStatement:=strSQL, _ Connection:="TABLE PrintQuery2" 
      .MailMerge.Execute Pause:=True
      .Application.Visible = True
      .MailMerge.Parent.Close False
    End With
    ...
    ...
    End Function
    '===========================================================================
    


    The above FAILS every time with Word hanging on the line: .MailMerge.OpenDataSource... !

    The FIX: Add a "TOP " clause to the SQL:
    "SELECT Top 1000 PrintQuery2.* from PrintQuery2"

    This works every time!

    Tested on Access 2010.
    Tested on Access 2016 C2R

    Brian, ProcessIT- Hawke`s Bay, New Zealand

    Sunday, March 25, 2018 11:13 PM