Need VBA Mail Merge Help!

timpak

New Member
Joined
Sep 23, 2015
Messages
4
So this has been a published code - (do not know who the author is) - for mail merge.

For my case, I have an excel file with line items and an email column and a purchase number column. I am using Microsoft Word and its Mail Merge function to populate a pre-drafted e-mail to send out to all of the line items, to its respective e-mail with all of the Merge Fields.

The code below is supposed to enable a variable subject for the e-mail. I want a dynamic subject line to include the purchase number in the subject for each e-mail. For example, the purchase number column heading is labeled Purch_Doc. And I want my e-mail subject line to say "Invoice Inquiry for Purchase Number <Purch_Doc>", so that the purchase number is variable in the subject line.

Probably the most simplest, but my problem currently: where do I put that subject line in the code? There were no clear instructions or notes on where I should put what I want for the subject in the code.

Thanks in advance!

-Tim


Code:
Dim WithEvents wdapp As Application
Dim EMAIL_SUBJECT As String
Dim FIRST_RECORD As Boolean


Private Sub Document_Open()

    Set wdapp = Application
    ThisDocument.MailMerge.ShowWizard 1
   
End Sub

Private Sub Document_Close()

    Set wdapp = Nothing
    
End Sub


Private Sub wdapp_MailMergeBeforeRecordMerge(ByVal Doc As Document, Cancel As Boolean)
    Dim i As Integer
    
    With ActiveDocument.MailMerge
    
        If FIRST_RECORD = True Then 
            EMAIL_SUBJECT = .MailSubject
            FIRST_RECORD = False
        Else .MailSubject = EMAIL_SUBJECT
        End If

        i = .DataSource.DataFields.Count
        
        Do While i > 0
            .MailSubject = Replace(.MailSubject, "<" & .DataSource.DataFields(i).Name & ">", .DataSource.DataFields(i).Value, , , vbTextCompare)
            i = i - 1
        Loop

    End With
    
     
End Sub


Private Sub wdapp_MailMergeBeforeMerge(ByVal Doc As Document, ByVal StartRecord As Long, ByVal EndRecord As Long, Cancel As Boolean)

    FIRST_RECORD = True
    
End Sub

Private Sub wdapp_MailMergeAfterMerge(ByVal Doc As Document, ByVal DocResult As Document)

    ActiveDocument.MailMerge.MailSubject = EMAIL_SUBJECT
     
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
The only code you need is:

Code:
Dim WithEvents wdapp As Application

Private Sub wdapp_MailMergeBeforeRecordMerge(ByVal Doc As Document, Cancel As Boolean)
    
    With ActiveDocument.MailMerge
        .MailSubject = "Invoice Inquiry for Purchase Number " & .DataSource.DataFields(2).Value
    End With
    
End Sub
where the 2 is the column number of the purchase number column in your Excel data source, i.e. column B in this example.

PS - put the code in the ThisDocument module.
 
Last edited:
Upvote 0
Sorry, you also need this code to initialise and destroy the wdapp event object:
Code:
Private Sub Document_Open()
    Set wdapp = Application
End Sub


Private Sub Document_Close()
    Set wdapp = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,358
Messages
6,124,487
Members
449,165
Latest member
ChipDude83

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