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

Word Print to PDF using VBA 1

Status
Not open for further replies.

mhypolite

Vendor
Feb 5, 2005
59
KY
Hi all, I am using PDFCreator, I want to create a macro that will automatically print the open word document and save it to a directory using a specified name with out being prompted for input, I have seen some Excel examples, can this be done in word? Or is there any other print to PDF program that works with word VBA

Thanks in advance.
 
I don't know PDF Creator but there are no options in Word or Excel for controlling its properties and its actions beyond printing with its printer driver. In Acrobat there are options which control whether it prompts for a filename, etc., but they are Acrobat options, not Word options - I presume PDF Creator is similar. If you have code which works in Excel there will be something very similar which works in Word - post it here if you need help adapting it.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
Hi Tony here is the exle code for example taken from
Option Explicit

Sub PrintToPDF_Early()
'Author : Ken Puls ('Macro Purpose: Print to PDF file using PDFCreator
' (Download from ' Designed for early bind, set reference to PDFCreator

Dim pdfjob As PDFCreator.clsPDFCreator
Dim sPDFName As String
Dim sPDFPath As String

'/// Change the output file name here! ///
sPDFName = "testPDF.pdf"
sPDFPath = ActiveWorkbook.Path & Application.PathSeparator

'Check if worksheet is empty and exit if so
If IsEmpty(ActiveSheet.UsedRange) Then Exit Sub

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
ActiveSheet.PrintOut copies:=1, ActivePrinter:="PDFCreator"

'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
End Sub
 
From a quick look at that code all you should need to do is change the Excel references to Word ones:

Change:

[tt] [/tt]sPDFPath = ActiveWorkbook.Path & Application.PathSeparator

to:

Code:
    sPDFPath = Active[red]Document[/red].Path & Application.PathSeparator

Change:

[tt] [/tt]If IsEmpty(ActiveSheet.UsedRange) Then Exit Sub

to something like:

Code:
    If len(ActiveDocument.content) =1 Then Exit Sub

Change:

[tt] [/tt]ActiveSheet.PrintOut copies:=1, ActivePrinter:="PDFCreator"

to

Code:
    ActivePrinter = "PDFCreator"
    Application.PrintOut Copies:=1

I may have missed something but, that aside, if it doesn't work post back and I'll check with Ken if need be.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
Hello Tony,. i have made the modifications that got me a bit further, but now, when activating the macro the job just sticks in the printer Que along with PDFCreator that also seem to be stuck, until i close Word.
 
Tony Thanks for the help, i got it, this is what i did
i add an extra Declaration, then i modified the print the document to pdf, and it all wors now.


Dim Old Printer



OldPrinter = Word.ActivePrinter
Word.ActivePrinter = "PDFCreator"

'Print the document to PDF

ActiveDocument.PrintOut Background:=False, Copies:=1
Word.ActivePrinter = OldPrinter
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top