MODERATOR NOTICE: This topic has also been posted on other sites and may already have an answer elsewhere. Please take this into consideration when answering this question
I'm looking for some help on a code query.
I've pulled some code together to run a mail merge. I initially ran the code via my office 365 Excel application and all worked fine. However, I needed to run the code on Office 2016 and there is one line of code that isn't functioning.
It's the line of code wdocSource.Close SaveChanges:=wdDoNotSaveChanges that did not function. The code stops running awaiting a manual input to the save/don't save dialogue box before the code then completes its run. It seems to ignore the code that is supposed to close the word document down without saving.
Any help would be appreciated.
- Sub RunMerge()
- Dim wd As Object
- Dim wdocSource As Object
- Dim strWorkbookName As String
- On Error Resume Next
- Set wd = GetObject(, "Word.Application")
- If wd Is Nothing Then Set wd = CreateObject("Word.Application")
- End If
- On Error GoTo 0
- Set wdocSource = wd.Documents.Open("C:\Users\macde\Desktop\Merge test\merge test.docx")
- strWorkbookName = ThisWorkbook.Path & "\" & ThisWorkbook.Name
- wdocSource.MailMerge.MainDocumentType = wdFormLetters
- wdocSource.MailMerge.OpenDataSource _
- Name:=strWorkbookName, _
- AddToRecentFiles:=False, _
- Revert:=False, _
- Format:=wdOpenFormatAuto, _
- Connection:="Data Source=" & strWorkbookName & ";Mode=Read", _
- SQLStatement:="SELECT * FROM `Sheet1$`"
- With wdocSource.MailMerge
- .Destination = wdSendToNewDocument .SuppressBlankLines = True
- With .DataSource
- .FirstRecord = wdDefaultFirstRecord
- .LastRecord = wdDefaultLastRecord
- End With
- .Execute Pause:=False
- With wd.Visible = True wdocSource.Close SaveChanges:=False
- Set wdocSource = Nothing Set wd = Nothing
- Dim wdApp As Word.Application
- Set wdApp = GetObject(, "Word.Application")
- wdApp.ActiveDocument.SaveAs "C:\Users\macde\Desktop\Merge test\final_letter.docx"
- End Sub