Excel 2007 VBA to Merge Selected Data into Word Merge Form

cvincent

Board Regular
Joined
Aug 28, 2011
Messages
66
I am attempting to utilize data from my Excel file to merge into a Word form. Users can use search buttons to filter selected data, which is then copied to a tab entitled "Selected Contacts". I can then run a macro that opens my Word 2007 merge document, but I am unable to create the code to run the actual merge with the "Selected Contacts" and then close the original merge document. Also, when it opens the Word merge document, it asks if it should be opened as read only, and I would like that done automatically without being asked. Any assistance would be appreciated. My code is:

Sub OpenWordDoc()

Dim wordApp As Object
Dim wordDoc As Object
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open("G:\EMERGENCY CONTACT MERGE DOC (6-2013).docm")
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
New VBA Code still does not work. Getting Application-defined or Object-Defined Error message 7121 at line " Set wordMailMerge = wordDoc.MailMerge". Learning VBA...need some assistance please.

Sub RunMailMerge()
'
' RunMailMerge Macro

Application.ScreenUpdating = True
Application.DisplayAlerts = False
Dim wd As Object
Dim wordDoc As Object
Dim wordocSource As Object
Dim wordApp As Object
Dim strWorkbookName As String

wordPath = ThisWorkbook.Path & "\EMERGENCY CONTACT MERGE DOC (6-2013).doc"
Set wordApp = CreateObject("Word.Application")
Set wordDoc = wordApp.Documents.Open(wordPath)
Set wordMailMerge = wordDoc.MailMerge


wordMailMerge.OpenDataSource Name:=excelPath, SQLStatement:="SELECT * FROM `'Selected Contacts$'`"
wordMailMerge.Execute
wordDoc.Close
wordApp.Visible = True

Worksheets("Selected Contacts").Visible = True

Set wordMailMergeFields = Nothing
Set wordMailMerge = Nothing
Set wordDoc = Nothing
Set wordApp = Nothing
Sheets(CurrentWorksheet).Select

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,928
Members
452,366
Latest member
TePunaBloke

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