Excel mail merge to Word Doc

Gavin23

New Member
Joined
Jul 10, 2014
Messages
18
Hi all
Been struggling a bit with a piece of code to try get my Word Doc to mail merge with data from my spreadsheet. Says it's having an issue with the last line i.e. "SQLStatement:="SELECT*FROM[Sheet2$]"

Assistance would be greatly appreciated - still new to working with this.

VBA Code:
Sub Open_Word_Document()
'On Error Resume Next

'Opens a Word Document from Excel

Dim objWord As Object

Set objWord = CreateObject("Word.Application")
objWord.Visible = True

'Change the directory path and file name to the location
'of your document

objWord.Documents.Open "C:\Users\gavin\Desktop\funding\funding.docx"

With objWord

.ActiveDocument.MailMerge.OpenDataSource Name:="C:\users\gavin\desktop\funding\funding_Summary.xlsm" _
, ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
Format:=wdOpenFormatAuto, Connection:="", _
SQLStatement:="SELECT*FROM[Sheet2$]"

With .ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
End With
'
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
SQLStatement:="SELECT*FROM[Sheet2$]"
I don't know if this is the issue, but I think you should at least have spaces in there, i.e.
VBA Code:
SQLStatement:="SELECT * FROM [Sheet2$]"
 
Upvote 0
What is the actual name of the Excel Sheet and Table you are trying to pull the data from?
 
Upvote 0
What is the actual name of the Excel Sheet and Table you are trying to pull the data from?
Workbook is called "Funding_Summary" and the sheet is called "Sheet2". If I open the Word doc itself, manually, then it asks to update details from "SELECT * FROM [Sheet2$]" and all is fine.....
 
Upvote 0
Workbook is called "Funding_Summary" and the sheet is called "Sheet2". If I open the Word doc itself, manually, then it asks to update details from "SELECT * FROM [Sheet2$]" and all is fine.....
So then is it working now?
 
Upvote 0
So then is it working now?
Only if I open the Word doc up separately. If I try run the code from within the spreadsheet it gives me the error...Selecting "yes" below pulls the data into the Word doc

Screenshot 2022-08-08 124243.jpg
 
Upvote 0
Gavin23, did you ever get this VBA to work, without manually opening Word?
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top