Private objWord As Word.Application
Private objDocument As Word.Document
Private Sub WritePDF(FileName As String)
Dim fName As String
Dim pName As String
Dim p As Printer
Dim Branch As String
Dim Path As String
Branch = "Software\Microsoft\Windows NT\CurrentVersion\Print\Printers\Acrobat Distiller"
Path = GetSettingString(HKEY_LOCAL_MACHINE, Branch, "Port", "")
If Path = "" Then
Path = "C:\Program Files\Adobe\Acrobat 4.0\PDF Output\"
Else
Path = Left$(Path, InStrRev(Path, "\"))
End If
On Error Resume Next
Set objWord = GetObject(, "Word.Application")
' if error then Word wasn't open
If Err.Number <> 0 Then
' open Word
Set objWord = CreateObject("Word.Application")
End If
Err.Clear
On Error GoTo 0
Set objDocument = objWord.Documents.Open(FileName)
' activate the document
objDocument.Activate
'Save the default printer name
pName = Printer.DeviceName
With objWord
On Error Resume Next
' I can not find a way to create the files where I want, and this
'is the default directory... so I delete all the PDF files, and
'LOG files first. Make sure this is not a problem.
Kill Path & "*.log"
Kill Path & "Microsoft Word - *.pdf"
On Error GoTo 0
'Set the Acrobat Distiller as the default printer
.ActivePrinter = "Acrobat Distiller"
'Print the File
.PrintOut FileName:="", Range:=wdPrintAllDocument, item:= _
wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
Collate:=True, Background:=True, PrintToFile:=False, PrintZoomColumn:=0, _
PrintZoomRow:=0, PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0
'Wait until the LOG file is created... this means the PDF has just
'been created
Do
DoEvents
Loop Until Len(Dir(Path & "Microsoft Word - *.log", _
vbArchive))
'Get the created PDF filename
fName = Dir(Path & "Microsoft Word - *.pdf", vbArchive)
'Create the PDF Filename out of the DOC filename
FileName = Trim$(FileName)
FileName = Left$(FileName, Len(FileName) - 4) & ".pdf"
'Copy the file to the same directory where the DOC was.
FileCopy Path & fName, _
FileName
Do Until Len(Dir(FileName, vbArchive))
DoEvents
Loop
MsgBox "The PDF has been created"
'Set the default printer to the original.
.ActivePrinter = pName
End With
'Close Word.
objWord.Quit
' clean up after our-selves
Set objWord = Nothing
Set objDocument = Nothing
End Sub