?
Solved

VBA Code for Word 2003 to convert document to PDF using Adobe Acrobat 9 Pro / Distiller

Posted on 2010-01-08
10
Medium Priority
?
1,921 Views
Last Modified: 2012-05-08
I am looking for a VBA code that converts the currently active document to a PDF. I tried a few solutions that I found on the web, but none was working. So I though I ask here from scratch..

Word 2003
Adobe Acrobat 9 Pro

Thanks!
0
Comment
Question by:stmoritz
9 Comments
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 26213093
You should just be able to print it to PDF.

ActivePrinter = "Adobe PDF"
    ActiveDocument.PrintOut

Open in new window

0
 
LVL 10

Expert Comment

by:MaduKp
ID: 26273483
Word Doc to PDF Conversion
Command line using VBScript and automation
http://www.suodenjoki.dk/us/productions/articles/word2pdf.htm
Refer: Appendix B
0
 

Author Comment

by:stmoritz
ID: 26273514
thanks. forgot to mention that in that code I also would like to define the pdf name and path in order to avoid the sure having to enter or click anything apart from starting the VBA code.
0
Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

 

Author Comment

by:stmoritz
ID: 26273525
@MaduKp: thanks. how can i integrate the VBScript in appendix B into a VBA Module? just copy it? I tried that one before posting my question here, but couldn't get it working... thanks for any help.
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 26274188
You can print to the Adobe PDF printer, however, you need to set the output filename programatically in form of a registry entry. You can find the details in this Adobe SDK document:
http://livedocs.adobe.com/acrobat_sdk/9.1/Acrobat9_1_HTMLHelp/Distiller_WinIntro.61.11.html (you can click on the icon in the upper left corner to get the navigation bar).
This is done as on a per application basis, so you need to create a registry key specifically for your application (that is so that you don't by accident intercept other documents that should go to the printer or to other destinations).

0
 

Author Comment

by:stmoritz
ID: 26277172
or is there a way similar to this code, that's the one I use for creating pdf's for my excel sheets, maybe it can be used for word with a few small changes? a first code defines the file name and path and then calls this code to create the pdf...

Public Function PrintSheetsToPDF( _
      ByVal SheetsToPrint As Variant, _
      ByVal PDFFilePath As String, _
      Optional ByVal ReorderSheets As Boolean _
   ) As Boolean

' Print the specified sheets to a PDF file in the order specified. Requires
' Adobe Acrobat 7.0 and a reference to Acrobat Distiller. Returns True if the
' print was successful, False otherwise.
'
' Syntax
'
' PrintSheetsToPDF(Sheets, PDFFilePath, [ReorderSheets])
'
' SheetsToPrint - Array of sheet names to be printed. The sheets included are
'   sorted in that order and then printed in one print job. When the printing
'   is complete the original order is restored.
'
' PDFFilePath - Full path to the PDF file.
'
' ReorderSheets - Pass True to reorder the sheets to be printed in the order
'   specified, False to not sort. optional. If omitted then False is assumed.
'
' Example
'
' Print sheets "Sheet4", "Sheet10", and "Sheet1" in that order:
'   PrintSheetsToPDF Array("Sheet4", "Sheet10", "Sheet1"), "C:\Output.PDF"
   
   Dim Errors As Boolean
   Dim OriginalActiveWorksheet As Object
   Dim OriginalOrderNames As Variant
   Dim Index As Long
   Dim PDFDistillerApplication As PdfDistiller
   Dim TempPFFilePathName As String
   Dim PDFLogPathName As String
   Dim Result As Long
   
   ' Normalize the sheets to print parameter
   If Not IsArray(SheetsToPrint) Then SheetsToPrint = Array(SheetsToPrint)
   For Index = LBound(SheetsToPrint) To UBound(SheetsToPrint)
      If TypeName(SheetsToPrint(Index)) = "Worksheet" Then SheetsToPrint(Index) = SheetsToPrint(Index).Name
   Next Index
   
   ' Normalize the output pdf file name
   If LCase(Right(PDFFilePath, 4)) <> ".pdf" Then PDFFilePath = PDFFilePath & ".pdf"
   
   ' Save the current active worksheet
   Set OriginalActiveWorksheet = ActiveSheet
   
   If ReorderSheets Then
   
      ' Save the current sheet order
      ReDim OriginalOrderNames(1 To ThisWorkbook.Sheets.Count)
      For Index = 1 To ThisWorkbook.Sheets.Count
         OriginalOrderNames(Index) = ThisWorkbook.Sheets(Index).Name
      Next Index
     
      ' Reorder the worksheets
      For Index = UBound(SheetsToPrint) To LBound(SheetsToPrint) Step -1
         If ThisWorkbook.Sheets(SheetsToPrint(Index)).Index > 1 Then
            ThisWorkbook.Sheets(SheetsToPrint(Index)).Move Before:=ThisWorkbook.Sheets(1)
         End If
      Next Index
   
   End If
   
   ' Print the worksheets
   
   TempPFFilePathName = Left(PDFFilePath, InStrRev(PDFFilePath, ".")) & "pf"
   PDFLogPathName = Left(PDFFilePath, InStrRev(PDFFilePath, ".")) & "log"
   On Error Resume Next
   Kill TempPFFilePathName
   Err.Clear
   ThisWorkbook.Worksheets(SheetsToPrint).PrintOut ActivePrinter:="Adobe PDF", PrintToFile:=True, Collate:=True, PrToFilename:=TempPFFilePathName
   If Err.Number <> 0 Then
      MsgBox "To prevent this error from occurring in the future, open the Properties window for the 'Adobe PDF' printer, click the command button 'Printing Preferences', and uncheck the option 'Do not send fonts to ""Adobe PDF""'. Before the changes will take effect Excel must be quit and restarted."
      Errors = True
   End If
   On Error GoTo 0

   If ReorderSheets Then
      ' Restore the original worksheet order
      For Index = 1 To ThisWorkbook.Sheets.Count
         If ThisWorkbook.Sheets(OriginalOrderNames(Index)).Index <> Index Then
            ThisWorkbook.Sheets(OriginalOrderNames(Index)).Move Before:=ThisWorkbook.Sheets(Index)
         End If
      Next Index
   End If
   
   ' Restore the original active worksheet
   OriginalActiveWorksheet.Activate

   If Not Errors Then
      ' Convert the postscript file to .pdf
      Set PDFDistillerApplication = New PdfDistiller
      Result = PDFDistillerApplication.FileToPDF(TempPFFilePathName, PDFFilePath, "")
      On Error Resume Next
      Kill TempPFFilePathName
      If Result = 1 Then Kill PDFLogPathName
      On Error GoTo 0
   End If
   
   PrintSheetsToPDF = Not Errors
   
End Function

Open in new window

0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 26277587
It looks like (I'm not a VB programmer) you are converting your file to PostScript first, and then you run the DIstisller on that PostScript file. That is no longer necessary by just setting the registry key. It's much easier than what you do, just give it a try.
0
 

Author Comment

by:stmoritz
ID: 26291943
http://www.excelguru.ca/node/95


works perfectly with pdf creator (freeware), no need for adobe for this purpose for me anymore...
0
 

Accepted Solution

by:
ee_auto earned 0 total points
ID: 32650669
Question PAQ'd, 250 points refunded, and stored in the solution database.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a previous article published here at Experts Exchange, Signature Image with Transparent Background (http://www.experts-exchange.com/Web_Development/Document_Imaging/A_12380-Signature-Image-with-Transparent-Background.html), I explained how to cre…
PaperPort is a popular document imaging/management product from Nuance Communications (http://www.nuance.com/). It is in widespread use by both individuals (http://www.nuance.com/for-individuals/by-product/paperport/index.htm) and businesses (http:/…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question