VBA to run mail merge for selected row in Excel data

bren1519

New Member
Joined
Sep 17, 2009
Messages
10
Hi there,
We have an excel spreadsheet where the person adds a row at a time of data and they are looking for a macro that will open the word document and run the mail merge for that data only. I've got the macro working that will run a mail merge but it runs it for all the data. Is it possible to add in the criteria of the selected row of data? Note that the code is what i've picked up online and modified for my files so if it needs modifying at all, please let me know. Thanks! Brenda

Code:
Sub RunMailMerge()

    Dim wdOutputName, wdInputName As String
    wdOutputName = "c:\users\bbrown\desktop\testresult.docx"
    wdInputName = "c:\users\bbrown\desktop\template.docx"


    ' open the mail merge layout file
    Dim wdDoc As Object
    Set wdDoc = GetObject(wdInputName, "Word.document")
    wdDoc.Application.Visible = True


    With wdDoc.MailMerge
         .MainDocumentType = wdFormLetters
         .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:=False
    Set wdDoc = Nothing


End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

Forum statistics

Threads
1,215,503
Messages
6,125,179
Members
449,212
Latest member
kenmaldonado

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