Thanks for reply.
I'm now using:
Code:
Sub test_update(docPath As String, dataSourcePath As String, sqlQuery As String)
Dim wdApp As Object
Dim wdDoc As Object
' Open Word application
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
wdApp.DisplayAlerts = False
If wdApp Is Nothing Then
Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0
' Check if Word application is open
If wdApp Is Nothing Then
MsgBox "Unable to open or create Word application.", vbExclamation
Exit Sub
End If
' Open the Word document
On Error Resume Next
'Set wdDoc = wdApp.Documents.Open(docPath)
wdApp.DisplayAlerts = False
Set wdDoc = wdApp.Documents.Open(docPath, False, True, False, , , , , , , , True) 'set object as Word document used as base
' Ignore specific merge file errors
If Err.Number <> 0 Then
Err.Clear
End If
On Error GoTo 0
If wdDoc Is Nothing Then
MsgBox "Unable to open the Word document: " & docPath, vbExclamation
Exit Sub
End If
wdApp.DisplayAlerts = True
' Update the data source
On Error GoTo HandleError
With wdDoc.MailMerge
.MainDocumentType = 0 ' wdFormLetters
.OpenDataSource Name:=dataSourcePath, _
ConfirmConversions:=False, _
ReadOnly:=True, _
LinkToSource:=True, _
AddToRecentFiles:=False, _
PasswordDocument:="", _
PasswordTemplate:="", _
WritePasswordDocument:="", _
WritePasswordTemplate:="", _
Revert:=False, _
format:=0, _
Connection:="Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=" & dataSourcePath & ";Mode=Read;Extended Properties=""HDR=YES;IMEX=1"";", _
SQLStatement:=sqlQuery
End With
' Check for errors during the data source update
If Err.Number <> 0 Then
' Handle known database engine error by ignoring it
Debug.Print "Error: " & Err.Number
If Err.Number = 112 Then
Err.Clear
Else
MsgBox "Error updating the data source: " & Err.Description, vbExclamation
wdDoc.Close SaveChanges:=False
Set wdDoc = Nothing
Set wdApp = Nothing
Exit Sub
End If
End If
' Save the document
wdDoc.Save
' Close the document without prompting to save changes
wdDoc.Close SaveChanges:=False
' Quit Word application if no other documents are open
If wdApp.Documents.Count = 0 Then
wdApp.Quit SaveChanges:=False
End If
' Release objects
Set wdDoc = Nothing
Set wdApp = Nothing
MsgBox "Mail merge data source updated and document saved.", vbInformation
Exit Sub
HandleError:
' Handle errors
If Err.Number <> 0 Then
' Ignore known errors related to database engine
Debug.Print "Error: " & Err.Number
If Err.Number = 123 Then
Resume Next
Else
MsgBox "An unexpected error occurred: " & Err.Description, vbExclamation
On Error Resume Next
If Not wdDoc Is Nothing Then wdDoc.Close SaveChanges:=False
If wdApp.Documents.Count = 0 Then wdApp.Quit SaveChanges:=False
Set wdDoc = Nothing
Set wdApp = Nothing
End If
End If
End Sub
As you can see I've added 'wdApp.DisplayAlerts = False' just before the line that kicks off the error messages, but it still displays them.
If it helps, below is the full sequence I have to click through to clear the errors and then be able to assign a data source (I've removed name of doc and data):