Reputation: 3153
Trying to do a mail merge in word from excel but getting a Object doesn't support this property or method
error. Word opens up fine but none of the fields are populating. I have tried with and without the connection
parameter.
Also, I thought the point of the connection
parameter is so it won't prompt you for the table in excel for the merge?
wordFile = ThisWorkbook.Path & "\" & "uyoic" & ".docx"
excelFile = ThisWorkbook.Path & "\" & ThisWorkbook.Name
Set appWord = CreateObject("Word.Application")
appWord.Visible = True
appWord.Activate
Set wrdfile = appWord.Documents.Open(wordFile)
Application.Wait Now + TimeValue("00:00:01")
wrdfile.MailMerge.OpenDataSource Name:=excelFile ' Connection:="list"
wrdfile.Execute True
I have references VBA, Excel 16 Obj, Office 16 Obj, Outlook 16 Obj, Microsoft Scripting Runtime, Word 16 Obj, and VBA Extensibility 5.3 added. Am I missing one?
Upvotes: 0
Views: 299
Reputation: 5696
You don't need another reference because you're not using them.
When using CreateObject you're invoking objects with late bind.
Some suggestions:
Read code's comments and adjust it to fit your needs
Option Explicit
Public Sub MailMerge()
Dim wordApplication As Object
Dim wordFile As Object
Dim wordFilePath As String
Dim excelFilePath As String
wordFilePath = ThisWorkbook.Path & "\" & "uyoic" & ".docx"
excelFilePath = ThisWorkbook.Path & "\" & ThisWorkbook.Name
Set wordApplication = CreateObject("Word.Application")
wordApplication.Visible = True
wordApplication.Activate
Set wordFile = wordApplication.Documents.Open(wordFilePath)
'Application.Wait Now + TimeValue("00:00:01")
' This next line will ask for the source in WordApp (uncomment the SQLStatement and adjust it)
wordFile.MailMerge.OpenDataSource Name:=excelFilePath ', SQLStatement:="SELECT * FROM `Sheet1$`" ' Connection:="list"
wordFile.MailMerge.Execute True ' -> Execute method belongs to MailMerge
End Sub
Let me know if it works.
Upvotes: 1