Has anyone tried opening a Mail Merging Word Document through Access VBA?

prabha_friend

Prabhakaran Karuppaih
Local time
Today, 10:16
Joined
Mar 22, 2009
Messages
1,032
1755790597525.png

Please share your way you resolved it. Thanks.
 
This error is simple. It might be as simple as moving the Word document to a trusted location or declaring its current location as trusted. The latter method involves the trust center section under File >> Options
 
This error is simple. It might be as simple as moving the Word document to a trusted location or declaring its current location as trusted. The latter method involves the trust center section under File >> Options
Tried the same Doc but still the issue persists...
 
Hi June, Thanks for the reply. But I just want to confirm that a pre-made merging document (kind of a template) cannot be opened through code? and does the merging has to happen dynamically every time?
 
Seems that manually opening a Word merge template requires responding to an initial prompt to confirm data source. Then have to initiate the merge operation. So I presume opening a Word merge template with Access VBA is not as simple as opening a basic Word document.

Found some old code in my db I created just for testing Word merge. It opens merge template and sets datasource. Uses Open method, not Add. Not getting the security warning.
 
Seems that manually opening a Word merge template requires responding to an initial prompt to confirm data source. Then have to initiate the merge operation. So I presume opening a Word merge template with Access VBA is not as simple as opening a basic Word document.

Found some old code in my db I created just for testing Word merge. It opens merge template and sets datasource. Uses Open method, not Add. Not getting the security warning.
.Documents.Open("C:\Users\arrow\Documents\Bill.docx")
also showing the same error message...
 
search Mail merge on this forum and you will see many discussions about this topic.
 
search Mail merge on this forum and you will see many discussions about this topic.
Searching... so far haven't found one which solves my issue. Anyway thanks. Let me go through the pending results I haven't read so far. Let me try through VSTO once. Thanks again.
 
I've been distributing the following function, along with other Access to Word automation routines, for the last 25 years or so. It's always been well received. Note that the function creates a temporary text file as the data source for the merge, rather than using the original query.

Code:
Public Function WordMerge(strQuery As String, _
                        strDataDoc As String, _
                        strMergeFile As String, _
                        Optional blnSuppressBlankLines As Boolean = True)
                      
    ' Merges data from query into Word document
    ' Accepts:  Name of Access query providing data for merge - String.
    '           Path to Word data file created from query - String.
    '           Path to Word document to be used for merge - String.
    '           Optional setting to suppress or show blank lines
    '           if data missing ( Default = True ) - Boolean
  
    Dim dbs As DAO.Database, rst As DAO.Recordset
    Dim qdf As DAO.QueryDef
    Dim prm As DAO.Parameter
    Dim wApp As Object
    Dim wDoc As Object
    Dim wActiveDoc As Word.Document
  
    Set dbs = CurrentDb
  
    Set qdf = dbs.QueryDefs(strQuery)
  
    For Each prm In qdf.Parameters
        prm = Eval(prm.Name)
    Next prm
  
    Set rst = qdf.OpenRecordset
  
    ' exit function if recordset is empty
    If rst.EOF Then
        MsgBox "No data to merge.", vbInformation, "Mail Merge"
        GoTo Exit_Here
    End If
  
    Set wApp = GetWordApp()
  
    ' close datasource document if open in Word
    For Each wDoc In wApp.Documents
        If wDoc.Path & "\" & wDoc.Name = strDataDoc Then
            wDoc.Close wdDoNotSaveChanges
        End If
    Next wDoc
  
    ' delete current Word data file.
    ' ignore error if file doesn't exist
    On Error Resume Next
    Kill strDataDoc
    On Error GoTo 0
  
    ' create new Word data file
    ExportToText strQuery, strDataDoc, ",", True
  
    'open word merge document
    Set wDoc = wApp.Documents.Open(strMergeFile)
      
    ' execute merge
    With wDoc.MailMerge
        .OpenDataSource strDataDoc
        .SuppressBlankLines = blnSuppressBlankLines
        .Destination = wdSendToNewDocument
        .Execute
    End With
    Set wActiveDoc = wApp.ActiveDocument
  
    ' show document in maximized window
    ShowWord wApp, wDoc, wActiveDoc
  
Exit_Here:
    Set rst = Nothing
    Set dbs = Nothing
  
End Function

PS: The above function calls the following function, in the same module, to show Word:

Code:
Private Sub ShowWord(wApp As Object, wDoc As Word.Document, wActiveDoc As Word.Document)

    wApp.Visible = True
    wApp.WindowState = wdWindowStateMaximize
    wDoc.Close (wdDoNotSaveChanges)
    wActiveDoc.Activate
    wApp.Activate

End Sub

PPS: It is of course necessary to create a reference to the Word object library
 
Last edited:

Users who are viewing this thread

Back
Top Bottom