VBA printing a mail merge letter from Access (1 Viewer)

jessss

Registered User.
Local time
Today, 13:48
Joined
Jan 17, 2010
Messages
29
[Solved] VBA printing a mail merge letter from Access

I was wondering if anyone could help. I have a program which opens a Word mail merge then prints each record into its own PDF file however I have an issue where half way through instead of closing the temp file it closes the main mail merge file which crashes the program

Code:
Sub DepPrint(objWord As Word.Document, strName1 As String, i As Long)
    Dim strDestFileName As String
    Dim strSourceFileName As String
    Dim str7ZipPath
    Dim strPassword
    Dim strCommand
    Dim s As String * 8
    Dim n As Integer
    Dim ch As Integer

    Dim DokName  As String   'ADDED CODE
    Dim doc1 As Word.Document
    
    With objWord.MailMerge
        .Destination = wdSendToNewDocument
        .SuppressBlankLines = True
        With .DataSource
            .FirstRecord = objWord.MailMerge.DataSource.ActiveRecord
            .LastRecord = objWord.MailMerge.DataSource.ActiveRecord
               DokName = .DataFields("Membership_Number_Customer_ID_and_Contr").Value
               DokPassword = .DataFields("DOBFormat").Value
            End With
        .Execute Pause:=False
    End With
   
    ActiveDocument.ExportAsFixedFormat OutputFileName:= _
        "P:\Source code and files\MEI Contract Documents\Contract Doc Letters\Temp File\" & DokName & " - " & strName1 & ".pdf", _
        ExportFormat:=wdExportFormatPDF

    ActiveDocument.Close savechanges:=False
    
    strDestFileName = "P:\Source code and files\MEI Contract Documents\Contract Doc Letters\Temp File\" & DokName & " - " & strName1 & ".zip"
    strSourceFileName = "P:\Source code and files\MEI Contract Documents\Contract Doc Letters\Temp File\" & DokName & " - " & strName1 & ".pdf"
    str7ZipPath = "C:\Program Files\7-Zip\7z.exe"
    strPassword = DokPassword
    'MsgBox strPassword & " - " & DokName
    strCommand = str7ZipPath & " -p" & strPassword & " a -tzip """ & strDestFileName & """ """ & strSourceFileName & """"
    Shell strCommand

    
    objWord.MailMerge.DataSource.ActiveRecord = wdNextRecord
End Sub
What the program should do is go through the mail merge records and for each one copy the record to a new doucment, print it to PDF then close the new document however it keeps closing the main mail merge document instead which crashes the program.

Does anyone know why the program would do this and if possible how I can fix it?

Thank you for any help in advance :D
 
Last edited:

bastanu

AWF VIP
Local time
Today, 06:48
Joined
Apr 13, 2010
Messages
1,401
try to add a dot in front of all ActiveDocument and let us know what happens:

. ActiveDocument.ExportAsFixedFormat OutputFileName:= _
"P:\Source code and files\MEI Contract Documents\Contract Doc Letters\Temp File" & DokName & " - " & strName1 & ".pdf", _
ExportFormat:=wdExportFormatPDF

Cheers,
Vlad
 

Users who are viewing this thread

Top Bottom