Run Word Mailmerge from Excel VBA

stockman256

New Member
Joined
Jul 25, 2010
Messages
8
I have an excel spreadsheet with VBA code to generate a list and data for a word mailmerge. I've gotten the following code to work but I'd like to be able to use a variable to insert the filename instead of having to use a hyperlink and put the filename in the code. Is there a way to do that?

'Print Letters
Application.DisplayAlerts = False
Letter = Sheets("Control").Range("B17").Value


ActiveWorkbook.FollowHyperlink Address:="D:\OneDrive\My Docs\Letter 1.docx", NewWindow:=True


Dim DocName As String

DocName = ActiveDocument.Name

With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.Execute
End With

ActiveDocument.PrintOut Range:=wdPrintAllDocument, Item:= _
wdPrintDocumentWithMarkup, Copies:=1, Pages:="", PageType:= _
wdPrintAllPages, Collate:=True, Background:=True, PrintToFile:=False, _
PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
PrintZoomPaperHeight:=0

ActiveDocument.SaveAs2 Filename:=Letter
ActiveDocument.Close
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
I have an excel spreadsheet with VBA code to generate a list and data for a word mailmerge. I've gotten the following code to work but I'd like to be able to use a variable to insert the filename instead of having to use a hyperlink and put the filename in the code. Is there a way to do that?

'Print Letters
Application.DisplayAlerts = False
Letter = Sheets("Control").Range("B17").Value


ActiveWorkbook.FollowHyperlink Address:="D:\OneDrive\My Docs\Letter 1.docx", NewWindow:=True


Dim DocName As String

DocName = ActiveDocument.Name

With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.Execute
End With

ActiveDocument.PrintOut Range:=wdPrintAllDocument, Item:= _
wdPrintDocumentWithMarkup, Copies:=1, Pages:="", PageType:= _
wdPrintAllPages, Collate:=True, Background:=True, PrintToFile:=False, _
PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
PrintZoomPaperHeight:=0

ActiveDocument.SaveAs2 Filename:=Letter
ActiveDocument.Close
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges

Try below i provided that your file path is static.

VBA Code:
Dim fileName, filePath As String
filePath = "D:\OneDrive\My Docs\"
fileName = Dir(filePath)

Application.DisplayAlerts = False

Letter = Sheets("Control").Range("B17").Value
ActiveWorkbook.FollowHyperlink Address:="D:\OneDrive\My Docs\" & fileName, NewWindow:=True

Dim DocName As String

DocName = ActiveDocument.Name

With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.Execute
End With

ActiveDocument.PrintOut Range:=wdPrintAllDocument, Item:= _
wdPrintDocumentWithMarkup, Copies:=1, Pages:="", PageType:= _
wdPrintAllPages, Collate:=True, Background:=True, PrintToFile:=False, _
PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
PrintZoomPaperHeight:=0

ActiveDocument.SaveAs2 fileName:=Letter
ActiveDocument.Close
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
 
Upvote 0
@earthworm that still has the path coded into the VBA code. I'm looking for a solution to reference a cell within the worksheet to get the entirety of the file location/name for the word mail merge file so that I can use different templates to mail different letters. That may not be possible as every solution I have found has the location in the code...

I am able to open a specific word file but then I can't get it to do the mail merge - or I can hardcode the filename in the code and do the mailmerge but to change it, I have to change the code every time.

I've got a marketing sequence of 6 different letters that are mailed to each prospect over a 6 month period. If I can't get this to work, I can write six macros and code the file location for each letter into it, but I'd like to be able to just reference a cell that has the filename/location in it for each letter.
 
Upvote 0
@earthworm that still has the path coded into the VBA code. I'm looking for a solution to reference a cell within the worksheet to get the entirety of the file location/name for the word mail merge file so that I can use different templates to mail different letters. That may not be possible as every solution I have found has the location in the code...

I am able to open a specific word file but then I can't get it to do the mail merge - or I can hardcode the filename in the code and do the mailmerge but to change it, I have to change the code every time.

I've got a marketing sequence of 6 different letters that are mailed to each prospect over a 6 month period. If I can't get this to work, I can write six macros and code the file location for each letter into it, but I'd like to be able to just reference a cell that has the filename/location in it for each letter.

The code that i have shared will open any file present in that folder.

How will computer know from where to fetch the file without giving path.
 
Upvote 0

Forum statistics

Threads
1,216,100
Messages
6,128,834
Members
449,471
Latest member
lachbee

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