Hi all
I'm trying to do a mail merge, save each document with the field data (ID Number.pdf) and then email each document to the corresponding email address in my DB.
The code I am using for the macro looks as follows:
Sub SaveEachDocument()
' SaveEachDocument Macro
'
Dim pdfjob As PDFCreator.clsPDFCreator
Dim sPDFName As String
Dim sPDFPath As String
Dim OldPrinter
OldPrinter = Word.ActivePrinter
Word.ActivePrinter = "Adobe PDF"
Application.DisplayAlerts = False
' assign x as the SaveAs Base value
x = "C:\MailMerge\"
With ActiveDocument.MailMerge.DataSource
.ActiveRecord = wdFirstRecord
Do Until .ActiveRecord = .RecordCount
ActiveDocument.SaveAs x & .DataFields("ID_No").Value & "_OfferDocument2009" & ".doc"
'ActivePrinter = "Adobe PDF"
'/// Change the output file name here! ///
sPDFName = .DataFields("ID_No").Value & "_OfferDocument2009" & ".doc"
sPDFPath = x
Set pdfjob = New PDFCreator.clsPDFCreator
With pdfjob
'If .cStart("/NoProcessingAtStartup") = False Then
' MsgBox "Can't initialize PDFCreator.", vbCritical + _
' vbOKOnly, "PrtPDFCreator"
' Exit Sub
'End If
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = sPDFPath
.cOption("AutosaveFilename") = sPDFName
.cOption("AutosaveFormat") = 0 ' 0 = PDF
.cClearCache
End With
'Print the document to PDF
ActivePrinter = "Adobe PDF"
ActiveDocument.PrintOut Background:=False, Copies:=1
'Wait until the print job has entered the print queue
Do Until pdfjob.cCountOfPrintjobs = 1
DoEvents
Loop
pdfjob.cPrinterStop = False
'Wait until the PDF file shows up then release the objects
Do Until Dir(sPDFPath & sPDFName) <> ""
DoEvents
Loop
pdfjob.cClose
Set pdfjob = Nothing
.ActiveRecord = wdNextRecord
Loop
End With
Word.ActivePrinter = OldPrinter
ActiveDocument.Close
Application.DisplayAlerts = True
End Sub
However, this code does not execute correctly and is also dependant on having pdf creator installed on the machine.
How do I modify it if using Adobe PDF or activePDF server or different PDF printer?
Would it be better to create an access database to do this?
Regards
Colin
I'm trying to do a mail merge, save each document with the field data (ID Number.pdf) and then email each document to the corresponding email address in my DB.
The code I am using for the macro looks as follows:
Sub SaveEachDocument()
' SaveEachDocument Macro
'
Dim pdfjob As PDFCreator.clsPDFCreator
Dim sPDFName As String
Dim sPDFPath As String
Dim OldPrinter
OldPrinter = Word.ActivePrinter
Word.ActivePrinter = "Adobe PDF"
Application.DisplayAlerts = False
' assign x as the SaveAs Base value
x = "C:\MailMerge\"
With ActiveDocument.MailMerge.DataSource
.ActiveRecord = wdFirstRecord
Do Until .ActiveRecord = .RecordCount
ActiveDocument.SaveAs x & .DataFields("ID_No").Value & "_OfferDocument2009" & ".doc"
'ActivePrinter = "Adobe PDF"
'/// Change the output file name here! ///
sPDFName = .DataFields("ID_No").Value & "_OfferDocument2009" & ".doc"
sPDFPath = x
Set pdfjob = New PDFCreator.clsPDFCreator
With pdfjob
'If .cStart("/NoProcessingAtStartup") = False Then
' MsgBox "Can't initialize PDFCreator.", vbCritical + _
' vbOKOnly, "PrtPDFCreator"
' Exit Sub
'End If
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = sPDFPath
.cOption("AutosaveFilename") = sPDFName
.cOption("AutosaveFormat") = 0 ' 0 = PDF
.cClearCache
End With
'Print the document to PDF
ActivePrinter = "Adobe PDF"
ActiveDocument.PrintOut Background:=False, Copies:=1
'Wait until the print job has entered the print queue
Do Until pdfjob.cCountOfPrintjobs = 1
DoEvents
Loop
pdfjob.cPrinterStop = False
'Wait until the PDF file shows up then release the objects
Do Until Dir(sPDFPath & sPDFName) <> ""
DoEvents
Loop
pdfjob.cClose
Set pdfjob = Nothing
.ActiveRecord = wdNextRecord
Loop
End With
Word.ActivePrinter = OldPrinter
ActiveDocument.Close
Application.DisplayAlerts = True
End Sub
However, this code does not execute correctly and is also dependant on having pdf creator installed on the machine.
How do I modify it if using Adobe PDF or activePDF server or different PDF printer?
Would it be better to create an access database to do this?
Regards
Colin