Home Case Studies MS Office Tips and Tricks Sample Code Helpful Videos Custom Development  Custom Training Join Us Contact Us
Search Our Site:  

Abobe Acrobat/MS Word VBA Code

I Get It! Development uses Adobe Acrobat® to produce newsletters, classroom materials, and most importantly invoices! The portable document format (PDF) is the de facto standard for sharing documents on the web. You can download Acrobat Reader for free, but in order to produce PDF files, you must purchase the full version of Acrobat. You must also set a reference to the Adobe library in the VB Editor before attempting to program. See the movie showing how to set the reference in Windows Media format or RealOne format for more information.

The code below looks at a constant called strFolderToSearch and searches for all Word documents in that folder. Remember to set it to the right folder on your system or use the Word dialog box to ask the user each time what the folder name is.  Under that folder you need PS Files and PDF Files.

C:\FilesToPrint
|------\PS Files\
|-------\PDF Files\

There is no way we know to print directly to an Adobe PDF file without being prompted for a file name. If you have lots of files, it gets old quickly. The workaround used here is to create a PostScript file first and then convert it to a PDF file.

The converted file names are written to the original Word document along with how long the jobs ran (well, and whether or not they ran... it's a little flaky).



Advertisement

Online Courses For CPE Credit
CPE Link is a new provider of web-based continuing professional education (CPE) for accounting professionals.


There are several places in here where we sort of hang around waiting... Earlier versions of this module sort of fell apart because Word was still printing while Acrobat Distiller was trying to convert the file.  In this version, we purposely disable background printing so that nothing happens until Word says it's done.

You will also see in the class module that there is a OnJobDone event which we use to make sure the job did get done and to see how long it took. We also use the event to delete the PostScript file. There was additional ugliness (in earlier versions!) with the macro trying to delete the file while Acrobat Distiller was still using it.

Why can't we all just get along?

If you have Office XP and you'd like to download the Word Doc with the standard module below and the Class Module it relies on, just save this document to your hard drive (right click, Save As): WordToPDF.doc. If you'd like to download the whole file structure and some sample docs, download this zip file. The files will run in an Office 2000 environment but it may be more flaky.

Your Acrobat Distiller printer driver may be named something other than "Acrobat Distiller," so if your system hangs while trying to set the ActivePrinter, go to Control Panel and see what it's called then change the module accordingly.

Oh yeah.... Don't put this file in the folder with the other Word docs, It'll try to print itself with some unsavory results...
 

Sub MakeWordPDFs()
'---------------------- Start module ------
'I Get It! Development provides programming examples
'for illustration only, without warranty either
'expressed or implied, including, but not limited to,
'the implied warranties of merchantability and/or
'fitness for a particular purpose. This article
'assumes that you are familiar with the programming
'language being demonstrated and the tools used to
'create and debug procedures. These examples assume that
'you have licensed copies of all relevant software installed
'on the machine upon which the examples will be run.

'This routine goes through a folder and prints all Word Docs
'in it to individual PDF files
'!!!!!!!!!!!!!!!!!
'It uses the following external reference libraries:
'Microsoft Office 10.0 Object Library (8.0 & 9.0 also work (I think!))
'Acrobat Distiller
'Go to Tools > References in the VB Editor and check them
'!!!!!!!!!!!!!!!!!

'Oh yeah.... Don't put this file in the folder with the other Word docs
'It'll try to print itself with some unsavory results...
Dim appDist As cAcroDist 'see class module
Dim strActivePrinter As String
'FoundFile is a string, but since it's used
'in a For Each...Next loop, it must be a variant
Dim FoundFile As Variant
Dim docCurrent As Document
Dim svInputPS As String
Dim svOutputPDF As String
Dim svJobOptions As String
Dim strBase As String
Dim Pause As Date

Const strFolderToSearch As String = "C:\FilesToPrint"

Set appDist = New cAcroDist
strActivePrinter = Application.ActivePrinter
Application.ActivePrinter = "Acrobat Distiller"
'We don't want to see the distiller window
appDist.odist.bShowWindow = False
'We are using Start/Done events. If we spool, they don't fire
appDist.odist.bSpoolJobs = False
'Application.Filesearch is part of the shared Office library
'The loop below searches for Word files in the above named strFolderToSearch
With Application.FileSearch
.NewSearch
.FileType = msoFileTypeWordDocuments
.LookIn = strFolderToSearch
.Execute

For Each FoundFile In .FoundFiles
'We open the doc read only so we don't mess it up (and close it w/o changes)
Set docCurrent = Documents.Open(FoundFile, ReadOnly:=True)
'Use the doc name to decide the pdf name
strBase = Left(docCurrent.Name, Len(docCurrent.Name) - 4)
svInputPS = strFolderToSearch & "\PS Files\" & strBase & ".ps"
svOutputPDF = strFolderToSearch & "\PDF Files\" & strBase & "_doc.pdf"
'Word can print to a PostScript file and name it, but it can't
'print to a PDF file and name it. And we DON'T want to name each
'file individually, thanks much.
'
'Make sure background is false, or Windows is still printing while
'Distiller is trying to open the file.
docCurrent.PrintOut outputFilename:=svInputPS, PrintToFile:=True, Background:=False

'Just for that little added extra bit of paranoia, closing a read-only file
'expressly without saving changes.
docCurrent.Close SaveChanges:=False

'This uses our distiller class mod to make the PDF file
'Upon successful completion, it deletes the PostScript file
Call appDist.odist.FileToPDF(svInputPS, svOutputPDF, svJobOptions)

'Distiller is SLOW. We have to sit here until the JobDone Event
'fires and changes blnFinished to true
Do While Not appDist.blnFinished
DoEvents
Loop

Next FoundFile

End With

'Clean up
Application.ActivePrinter = strActivePrinter

Set appDist = Nothing
Set docCurrent = Nothing

End Sub

Class Module cAcroDist

Public WithEvents odist As PdfDistiller
Public blnFinished As Boolean
Dim StartTime As Date

Private Sub Class_Initialize()
Set odist = New PdfDistiller
End Sub

Private Sub odist_OnJobDone(ByVal strInputPostScript As String, ByVal strOutputPDF As String)
ActiveDocument.Content.InsertAfter strOutputPDF & " printed successfully at " & Now() & vbCrLf
ActiveDocument.Content.InsertAfter "Job took " & DateDiff("s", StartTime, Now()) & " seconds." & vbCrLf & vbCrLf
blnFinished = True
Kill strInputPostScript
End Sub

Private Sub odist_OnJobFail(ByVal strInputPostScript As String, ByVal strOutputPDF As String)
ActiveDocument.Content.InsertAfter strOutputPDF & " failed to print at " & Now() & vbCrLf
ActiveDocument.Content.InsertAfter "Job took " & DateDiff("s", StartTime, Now()) & " seconds." & vbCrLf & vbCrLf
blnFinished = True
End Sub

Private Sub odist_OnJobStart(ByVal strInputPostScript As String, ByVal strOutputPDF As String)
StartTime = Now()
ActiveDocument.Content.InsertAfter strOutputPDF & " is printing " & Now() & vbCrLf
blnFinished = False
End Sub

 

 
Please share your comments on this article!

 
Name (opt)
Email Address (opt)
Your Questions/
Comments

I Get It! Development does not share your email address or other information, nor do we add you to any mailing lists unless you specifically request to be added by checking the box below.



SEE ALSO
You may also find the following articles useful:

Calculate Growth Rates In Excel

Spreadsheet vs. Database

Pimp My Spreadsheet!

Columns In Word

Bound vs Unbound Forms

Subqueries In Access

Amazing Excel Comments

Deduplicate Excel Data

Excel - Access Import Problems

Circular References

Custom Formats In Excel

Columns In Access

Auto Update Charts

Data Quick Check In Excel

Advanced Sorting In Excel

References

Journal of Accountancy Articles

Tweaking The Numbers

Block That Spreadsheet Error

Excel Security Issues


 

Clients

Do you need help with Microsoft Office? Contact us

See how we've helped companies like yours develop effective business processes

Client Success Stories


 

Consultants

I Get It! Consultants set their own hours, work with their own clients, and choose their own projects.

If you're a Microsoft Office guru and would like to explore being a full-time consultant, please see our consultant information pages.