PDF In-Depth

IAC - Automating batch Word DOC conversion to PDF using 'Acrobat Distiller' printer

May 12, 2000

Advertisement
Advertisement
 

Here is some VB code that will allow you to create PDF documents in batch using Acrobat 4's "Acrobat Distiller" printer. Your files will be deposited in the Acrobat 4 - PDF Output folder, usually c:\Program Files\Adobe\Acrobat 4.0\PDF Output

This is a simple demonstration of how all Word files in a given folder can be converted to PDFs. The Acrobat Distiller printer (installed with Acrobat 4) must be configured and operational for this to work. No bookmarks or hyperlinks are added - just plain old vanilla PDF. You'll also notice that the printername is hardcoded. It would be a good idea to have this set as a variable. Use this as you see fit, however, be aware it hasn't been tested for scalability or reliability.

Brief Instructions

Create a new VB project, add a button called "btnConvert", an edit control called "m_txtSource", and paste the following code into the form code section.

To run the program, type name of the folder with your PDFs into the edit control and click the convert button.

*** Don't forget to reference the Word.8 library in your project
*** You'll want to make sure that Distiller is not setup to prompt 
*** you for filenames

'///////////////////////////////////////////////////
'///////////////////////////////////////////////////

Function ConvertFile(strSourceFileName As String) As String
On Error GoTo ErrorHandler

Dim msWord As Word.Application
Set msWord = GetObject(Class:="Word.Application.8")

msWord.Visible = True
msWord.ActivePrinter = "Acrobat Distiller"
msWord.Documents.Open strSourceFileName
msWord.ActiveDocument.PrintOut
msWord.ActiveDocument.Close False

' Should check and quit word when done
Set msWord = Nothing
ConvertFile = True
Exit Function

'////////////////////////////////////////////////////
ErrorHandler:
' Create Word for the first time if it is not active
If Err.Number = 429 Then
Set msWord = CreateObject("Word.Application.8")
Err.Clear ' Clear Err object in case error occurred.
Resume
End If

' All other errors handled here
If IsCriticalError Then
    ConvertFile = False
    Exit Function
Else
    Resume
End If
End Function

'///////////////////////////////////////////////////
'///////////////////////////////////////////////////

Private Function IsCriticalError() As Boolean
      Dim strErrorMessage As String
      Select Case Err.Number ' Evaluate error number.
           Case Else
                  strErrorMessage = "Please contact 

info@planetpdf.com and inform them that" & Chr$(13) & _
                  "the error message reported by the operating system 

was " & Chr$(13) & _
                  Chr$(34) + Trim(Str(Err.Number)) & " " & 

Err.Description + Chr$(34)
                  MsgBox strErrorMessage, , "Conversion error" + 

Str(Err.Number)
           IsCriticalError = True
           Exit Function
      End Select
      IsCriticalError = False
End Function

'///////////////////////////////////////////////////
'///////////////////////////////////////////////////

Private Sub btnConvert_Click()
Dim strFileToConvert As String
Dim strFolder As String

' Set the source folder
strFolder = m_txtSource

' Grab the first file to convert
strFileToConvert = Dir(strFolder + "*.doc")

' Loop through all word files
While strFileToConvert <> ""
   
      ' Attempt to convert the file to PDF
      If (ConvertFile(strFolder + strFileToConvert) = False) Then
           ' Hmmm, looks like something went wrong - let's prompt the 
		user to see if they wish to quit
           If (MsgBox("There has been a problem converting the file " + 

strFileToConvert + chr(13) + "Do you wish to exit", vbYesNo) = vbYes) Then
           ' Finish up - let's get out of here
                  Exit Sub
           End If
      End If
    
      ' Grab the next file
           strFileToConvert = Dir
Wend

End Sub

'///////////////////////////////////////////////////
'///////////////////////////////////////////////////
PDF In-Depth Free Product Trials Ubiquitous PDF

Debenu Quick PDF Library

Get products to market faster with this amazing PDF developer SDK. Over 900 functions and an equally...

Download free demo

Back to the past, 15 years ago! Open Publish 2002

Looking back to 2002, it's amazing how much of the prediction became a reality. Take a read and see what you think!

September 14, 2017
Platinum Sponsor





Search Planet PDF
more searching options...
Planet PDF Newsletter
Most Popular Articles
Featured Product

Debenu PDF Aerialist

The ultimate plug-in for Adobe Acrobat. Advanced splitting, merging, stamping, bookmarking, and link control. Take Acrobat to the next level.

Features

Adding a PDF Stamp Comment

OK, so you want to stamp your document. Maybe you need to give reviewers some advice about the document's status or sensitivity. This tip from author Ted Padova demonstrates how to add stamps with the Stamp Tool along with related comments.