Link to home
Start Free TrialLog in
Avatar of mlcktmguy
mlcktmguyFlag for United States of America

asked on

MailMerge And Create PDF

I have some code from EE that will generate Mail Merge output using an Access Table and a Word Doucment.

Public Sub CreateMailMerge_EE2()

'open the mergedoc
        Dim oApp As Object
        Dim MlMrge As Object
        Set oApp = CreateObject(Class:="Word.Application")
        oApp.Visible = True
        
        'this will open a new document based on the path as a template.  Excellent
        'but it doesn't open it as a mail merge
        oApp.Documents.Add "C:\My Documents\Access_Databases\JordanDelinq\MailMergeTestDoc.docx"
        ' so start a merge
        Set MlMrge = oApp.ActiveDocument.MailMerge
        With MlMrge
            'do the merge, get the datasource -- and be tricky if the Access app is opened exclusively!
            '
            ' Original //////////////////////////////////////////////
            '
            .OpenDataSource Name:="C:\My Documents\Access_Databases\JordanDelinq\MailTestData.accdb", _
            LinkToSource:=True, AddtoRecentFiles:=False, _
            Connection:="TABLE [tblNameFile]", _
            SQLStatement:="SELECT * FROM [tblNameFile]"
            '
            '/////////////////////////////////////////////////////////////
            '
            
            
            
             'show the field values
            .ViewMailMergeFieldCodes = 9999998 'wdToggle
            'merge to a new document
             'do you want merge to email instead
            .Destination = 0 'wdSendToNewDocument
            .SuppressBlankLines = True
            'blow through all the records
            With .DataSource
                .FirstRecord = 1 'wdDefaultFirstRecord
                .LastRecord = -16 'wdDefaultLastRecord
            End With
            .Execute (False) 'execute and don't stop for errors -- list them in a new Word document, if any.
        End With


End Sub

Open in new window


I've tested the code an it works, presenting the complete mail merged document on the screen.

The customer also wants to be able to create a PDF, so certain generated documents are stored and associated with each account.

To execute the mail merge record by record I realize I'll have to change the select to only pull one record at a time and pass the recordID as a parameter.  I'm planning to use an outside loop that reads thru tblNameFile pulling the pertinent records and calling the mail merge routine with a specific record ID.

I'll change the select to :  SQLStatement:="SELECT * FROM [tblNameFile] Where ID = " & passedID

That way one record at a time will run though the mail merge.

Within the above loop how would I create a PDF for each tblNameFile record currently being processed?
How would I link the PDF to the tblNameFIleRecord being processed?

Is there a better way to accomplish this that using the current code loop I am using now?
ASKER CERTIFIED SOLUTION
Avatar of Luke Chung
Luke Chung
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mlcktmguy

ASKER

Thanks but not interested in including any 3rd party products in this project.