Word Mail Merge - adding text to email body

Hello,

I have vba email mail merge code that works without issues.
Using the code below, how would I add text to the bottom of the mail merge email message?

    Windows(DocName).Activate
   
    ' Do mailmerge
    ' Only send if an email address exists.
    With ActiveDocument.MailMerge
        .MainDocumentType = wdEMail

        'Specify the data source here
        .OpenDataSource Name:=strDataSource & "\" & DocNameCut & ".dbf", _
            ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
            AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
            WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
            Format:=wdOpenFormatAuto, Connection:= _
            "Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=" & strDataSource & ";" & _
            "Mode=Read;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";" & _
            "Jet OLEDB:Engine Type=18;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=" _
            , SQLStatement:="SELECT * FROM `" & DocNameCut & "` WHERE EMAIL > ''", SQLStatement1:="", SubType:=wdMergeSubTypeAccess
           
        If .DataSource.RecordCount > 0 Then

            .MailSubject = "Notice RE: " & .DataSource.DataFields("CLIENT").Value
            .MailFormat = wdMailFormatHTML
            .Destination = wdSendToEmail
            .MailAddressFieldName = "EMAIL"
            .SuppressBlankLines = True
           
            With .DataSource
                 .MappedDataFields(wdFirstName).DataFieldIndex = .DataFields("DFNAME").Index
                 .MappedDataFields(wdLastName).DataFieldIndex = .DataFields("DLNAME").Index
                 .MappedDataFields(wdUniqueIdentifier).DataFieldIndex = .DataFields("CUSTNUM").Index
                 .MappedDataFields(wdAddress1).DataFieldIndex = .DataFields("DADDR1").Index
                 .MappedDataFields(wdCity).DataFieldIndex = .DataFields("DCITY").Index
                 .MappedDataFields(wdState).DataFieldIndex = .DataFields("DPROV").Index
                 .MappedDataFields(wdPostalCode).DataFieldIndex = .DataFields("DPCODE").Index
                 .MappedDataFields(wdCountryRegion).DataFieldIndex = .DataFields("DCOUNTRY").Index
                 .MappedDataFields(wdEmailAddress).DataFieldIndex = .DataFields("EMAIL").Index
                 .MappedDataFields(wdSpouseFirstName).DataFieldIndex = .DataFields("SPOUSEFNAM").Index
                 .MappedDataFields(wdSpouseLastName).DataFieldIndex = .DataFields("SPOUSELNAM").Index
                 .FirstRecord = wdDefaultFirstRecord
                 .LastRecord = wdDefaultLastRecord
             End With
             .Execute Pause:=False
        End If
    End With

Thanks,

CJGibson

Download the MergeTools – 20140218 Add-in that I created from the following page of my One Drive\Windows Live SkyDrive:

https://onedrive.live.com/?cid=5aedcb43615e886b#cid=5AEDCB43615E886B&id=5AEDCB43615E886B%21566

https://skydrive.live.com/?cid=5aedcb43615e886b#!/?cid=5AEDCB43615E886B!cid=5AEDCB43615E886B&id=5AEDCB43615E886B%21566

Save the file in the Word Startup folder.  In Windows XP the default location for that folder is

C:\Documents and Settings\[User Name]\Application Data\Microsoft\Word\STARTUP

In Windows Vista and Windows 7, it is

C:\Users\[User Name]\AppData\Roaming\Microsoft\Word\STARTUP

When you have done that, the tab shown below will be added to the Ribbon:

One thing to note is that the field names in your data source must contain only alphanumeric characters (No @,#,$,%,&,(,), etc) and the field names must not start with a numeric character (0-9).

You may also want to download the Merging with Attachments document that is also on that page which explains how the system is used.  It is not actually necessary to have separate attachments as the facility can be used to send just the documents created by the merge itself as attachments, either in the form of Word files or .pdf files.

Hope this helps,
Doug Robbins - MVP Office Apps & Services (Word)
dougrobbinsmvp@gmail.com
It's time to replace ‘Diversity, Equity & Inclusion’ with ‘Excellence, Opportunity & Civility’ - V Ramaswamy

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Hi Doug,

Thanks for the reply although I don't see how it really helps me.

After looking and trying the merge tools,  I'm not really sure what anything does and if this can be accessed through code.  I even found your document Merging with Attachments confusing.

I tried clicking the icons of your merge tools; however, I kept getting the message that I have to use letter type main document.

I wasn't looking for an alternative to Word's mail merge, just how to add text to the body of the email with the VBA code I provided.

CJGibson

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

There is no way that you can modify that code to do what you want.  In fact, I am not sure why you are using code if you are not looking for an alternative to Word's mail merge.

If you are executing a merge to email, the text will need to be in the mail merge main document and if you want variable text, you could use an { IncludeText } field that references a field in the data source that contains the variable text.

On the other hand, if you use the Merge with Attachments facility on my MergeTools add-in to execute the "merge" to a Word or PDF Attachment, you can enter text into what will be the covering email message that is sent to the recipients with their "merged" document as the attachment.

Hope this helps,
Doug Robbins - MVP Office Apps & Services (Word)
dougrobbinsmvp@gmail.com
It's time to replace ‘Diversity, Equity & Inclusion’ with ‘Excellence, Opportunity & Civility’ - V Ramaswamy

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Hi Doug,

Once again thanks for the reply.

Perhaps either you misunderstood my request or I haven't communicated my thought clear enough.

According to your documentation, your MergeTools seems to take the data source from an excel file.  I am using a dbf file.

I am in no way looking for an alternative to Word's mail merge.  I am performing the merge via code so that the merge can progress several ways depending on the user's actions.  This is an automated process, not a manual one.

Let me provide some more information.

I have a batch file that calls a word document.  Using VBA code, after the data source is accessed, if the email field has an email address, then the code above is executed, otherwise a slight variation of the code is executed to either allow the user to view the merged documents or to send them to a printer.

In the original word document, I had a footer with address information.  I have recently learned that header and footer information is ignored when performing email mail merge. 

I found some code to allow me to extract the text from the footer except I didn't know how to add that code to the email body using my existing code as displayed above.  Apparently, according to you, this is not possible unless I am accessing data from the data source.  This information won't be available from the data source.

I found an alternative solution that seems to work.

I placed the text from the footer into a text box, then vertically positioned the textbox to the bottom relative to the page.  This way it still looks like a page footer, although it isn't actually a footer.

The text in this textbox appears exactly as expected whether the page is viewed, printed or emailed.

Thanks again for your replies.

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 3,054 Applies to: