[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
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
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
Does anyone know why the program would do this and if possible how I can fix it?
Thank you for any help in advance

Last edited: