Knowledgebase: Developer
Automating creating PDFs from Word
Posted by admin admin on 21 November 2006 02:34 PM
Problem:
How can I automate printing a Microsoft Word document to Win2PDF?

Solution:
The following sample code uses Microsoft Word and Win2PDF to create PDF files. The sample code bypasses Win2PDF's File Save dialog.
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Dim WdApp As Object
Set WdApp = CreateObject("Word.Application")

'save path and file name in registry for Win2PDF, to bypass file prompt
SaveSetting "Dane Prairie Systems", "Win2PDF", "PDFPostFileName", "c:\myfile.pdf"

With WdApp
.Documents.Open FileName:="c:\myfile.doc"
.Options.PrintBackground = True
.ActivePrinter = "Win2PDF"
.Visible = False
.ActiveDocument.PrintOut
.Documents.Close savechanges:=False
End With

'Verify Win2PDF is done creating PDF file
Do
  Sleep 10
  PDFRegKey = GetSetting("Dane Prairie Systems", "Win2PDF", "PDFPostFileName", "")
Loop While PDFRegKey <> Empty
(0 vote(s))
Helpful
Not helpful

Comments (4)
Charlene Schaffer
28 January 2009 11:37 AM
When I use this code to print my word doc to a PDF, the macro runs as it should, but the PDF cannot be found...
Charlene Schaffer
30 January 2009 10:21 AM
This changed my registry permanently. Now everything I print to a PDF gets printed using the file path and name that I entered. I need help...
Lokoman
30 September 2011 10:07 AM
The "<>" is missing in the DO...LOOP: This is the correct code:

'Verify Win2PDF is done creating PDF file
Do
Sleep 10
PDFRegKey = GetSetting("Dane Prairie Systems", "Win2PDF", "PDFPostFileName", "")
Loop While PDFRegKey <> Empty
Win2PDF support
18 January 2013 03:32 PM
The missing "<>" has been corrected in the article.

Copyright 2000-2018 © Dane Prairie Systems LLC. All Rights Reserved. About Us | Privacy Policy | PDF Blog | Site Map