Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change Word MailMerge OpenDataSource

Status
Not open for further replies.

Bluejay07

Programmer
Mar 9, 2007
780
CA
Hello,

I have a macro that performs a mail merge and depending on the datasource, the processing of the merge is handled differently.
The first step is to print notices that do not have an email address.
The second step is to email notices that have an email address.

When I am trying to process step 2, the datasource isn't being updated and an email is delivered with the data from step 1.
I've read that one can use 'wdNotAMergeDocument' to unlink the datasource, however, it doesn't seem to be working.
I've tried placing it at the end of the first section and also at the start of the second section. Neither seemed to help.

Here is some abbreviated code:
Code:
    ' Do mailmerge
    ' Only print those that do not have an email address.
    With ActiveDocument.MailMerge
        .MainDocumentType = wdFormLetters
    ...
    End With

    ' Do mailmerge
    ' Only send if an email address exists.
    With ActiveDocument.MailMerge
        .MainDocumentType = wdNotAMergeDocument
        .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

I should point out that before I added the wdNotAMergeDocument, if I commented out either step and ran the macro, the results are as expected. The proper document information is either printed or emailed. The problem is when they are both active and processed one immediately after the other.

How can I have two difference datasources in the same macro?

Thanks.

If at first you don't succeed, then sky diving wasn't meant for you!
 
Since the first section was using ".Destination = wdSendToNewDocument", I used "ActiveDocument.Close wdDoNotSaveChanges" then reset the activedocument and was able to continue without an issue.

If at first you don't succeed, then sky diving wasn't meant for you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top