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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print as a PDF file from a Mail Merge Word file

Status
Not open for further replies.

derricklo1980

IS-IT--Management
Jul 9, 2012
11
HK
Hi,

(1) I tried to use below code to print as PDF from a Word file. I can generate the file, but the file is not a real PDF file (you may refer the attached link file). Any idea?

Sub SetDefaultPrinter()

Dim sCurrentPrinter As String
On Cancel GoTo Cancelled:
' Get the current printer and store the value in sCurrentPrinter
sCurrentPrinter = ActivePrinter
' Set the printer to a PDF converter,
' here I use a freeware CutePDF Writer
ActivePrinter = "CutePDF Writer"
Application.PrintOut OutputFileName:="outputfilename.pdf", _
Item:=wdPrintDocumentContent, PrintToFile:=True
Cancelled:
' Restore the original printer
ActivePrinter = sCurrentPrinter

End Sub


(2) The purpose I don't use Save As as an option to export PDF is that, the Word file is a Mail Merge file which generates several letters, I want to have one combined PDF instead of several PDF files for each record. Any code can achieve this if the above doesn't work?

Thanks!
 
Your output file is a postscript file, not a PDF. I don't use Cute PDF, but I suspect that's because you have 'PrintToFile:=True'. Simply changing the extension to .ps and opening it with Adobe Acrobat Pro generates a PDF with 'Test print as pdf'.

Cheers
Paul Edstein
[MS MVP - Word]
 
I'd try looping thru the doc on Sections, DELETING the Section Breaks. THEN SaveAs.

Maybe just search for the breaks and DELETE them.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
I'm not Word VBA or otherwise, but I know that in the Word APPLICATION (not VBA) you can REPLACE ^b with ^m, which replaces Section Break with Page Break.

Record it.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
I use below code and can successfully generated a merged PDF file. Some problems:
(1) This will close the orginial Word template document, but generate a new
Word document. How can I keep the orginial Word doc open and close the
generated Word file after running the code?
(2) How can I write the total no of pages and no of records to a newly
generated file, like *.txt as a control/audit trail file? Thanks!


Public Sub MergePDF()

' This macro mail merges the data
' and prints to 1 PDF file

' This speeds up the macro.
Application.ScreenUpdating = False
Application.DisplayAlerts = False


' This does a mail merge with the word Document.
With GetObject("C:\word3.docm")
With .MailMerge
.MainDocumentType = wdFormLetters
.OpenDataSource Name:= _
"C:\excel3.xlsx", _
ConfirmConversions:=False, _
ReadOnly:=True, LinkToSource:=True, _
AddToRecentFiles:=False, _
PasswordDocument:="", _
PasswordTemplate:="", _
WritePasswordDocument:="", _
WritePasswordTemplate:="", _
Revert:=False, _
Format:=wdOpenFormatAuto, _
Connection:= _
"Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data
Source=C:\excel3.xlsx;Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Jet
OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Engine
Type=37;Jet OLE" _
, SQLStatement:="SELECT * FROM `Sheet1$`", SQLStatement1:="", SubType:=
_
wdMergeSubTypeAccess
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
.Execute False
End With

' This prints to a PDF file and
' saves it to a designated folder
pdfname = "TestMergePDF-" & ".pdf"
.Application.Documents(1).ExportAsFixedFormat pdfname, 17
.Close 0

End With

End
Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top