Hi All,
I am trying to run a simple mail merge using a quick access toolbar button to open an existing word template with all merge fields set up ready to go and already pointing to an excel data template. This procedure is done after first running another macro that sets up and saves the excel data template. I hope to combine the two macros into one once I can get the mail merge macro to function.
The excel data template is located at: G:\CAT Post Sale Quote Request Mail Merge\Post Sale Ship Request Data Template.xlsx
The word mail merge template is located at: G:\CAT Post Sale Quote Request Mail Merge\Post-sale Ship Request Email Template.docx
I have the following code that I do not fully understand except for the possible error in placement of the excel and word file locations:
Sub RunMailMerge()
Dim wdOutputName, wdInputName As String
wdOutputName = ThisWorkbook.Path & "G:\CAT Post Sale Quote Request Mail Merge\Post-sale Ship Request Email Template.docx"
wdInputName = ThisWorkbook.Path & "G:\CAT Post Sale Quote Request Mail Merge\Post Sale Ship Request Data Template.xlsx"
' open the mail merge layout file
Dim wdDoc As Object
Set wdDoc = GetObject(wdInputName, "Word.document")
wdDoc.Application.Visible = True
With wdDoc.MailMerge
.MainDocumentType = wdFormLetters
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
.Execute Pause:=False
End With
' show and save output file
wdDoc.Application.Visible = True
wdDoc.Application.ActiveDocument.SaveAs wdOutputName
' cleanup
wdDoc.Close SaveChanges:=False
Set wdDoc = Nothing
End Sub
I have trawled for days and clicked many links looking for the answer to this. The above code appeared to be the most straight forward approach if only I could adapt it to my file locations.
I'm not sure why I would need to access the excel file as the word template already looks for the data file.....
Can someone please point out where I am going wrong and/or what it is I am missing?
Many thanks,
Bookmarks