When I record using word macro, I can get the following mail merge with each record appending as table below one another.
Sub Macro2()
'
' Macro2 Macro
'
'
ActiveDocument.MailMerge.MainDocumentType = wdCatalog
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
End Sub
However, when I executive this from an excel as below, the word document did not seem to get it as "Catalog but just append each table using a new page just like using wdFormletters. Could anybody advise? Thank you!
Sub RunMailmergeWordGridtable()
Dim wdOutputName, wdInputName, Source As String
wdOutputName = ThisWorkbook.Path & "\Merged" & "\Word_merged.docx"
wdInputName = ThisWorkbook.Path & "\Word schedule.docx"
Source = ThisWorkbook.Path & "\XXX.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 = wdCatalog
.OpenDataSource Name:=Source, SQLStatement:="SELECT * FROM `Raw$`", SQLStatement1:=""
.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:=True
Set wdDoc = Nothing
End Sub
Bookmarks