Function Word2PDF(sDoc As String, sPDF)
On Error GoTo EH_Word2PDF
Dim oPDF As PDFCreator.clsPDFCreator
Dim DefaultPrinter As String
Dim oWord As Word.Application
Dim OutputFilename As String
Dim c As Integer
'Create Instances
Set oPDF = New clsPDFCreator
Set oWord = CreateObject("Word.Application")
Word2PDF = ""
'Open document
oWord.Documents.Open sDoc
With oPDF
.CStart "/NoProcessingAtStartup"
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = oWord.ActiveDocument.Path
.cOption("AutosaveFilename") = sPDF
.cOption("AutosaveFormat") = 0
DefaultPrinter = .cDefaultPrinter
.cDefaultPrinter = "PDFCreator"
oWord.PrintOut Background:=True
.cClearCache
.cPrinterStop = False
End With
c = 0
Do While (oPDF.cOutputFilename = "") And (c < (maxTime * 1000 / sleepTime))
c = c + 1
Sleep 500
Loop
OutputFilename = oPDF.cOutputFilename
With oPDF
.cDefaultPrinter = DefaultPrinter
Sleep 1000
.cClose
End With
Sleep 2000 ' Wait until PDFCreator is removed from memory
sPDFDoc = OutputFilename
oWord.Quit False
If OutputFilename = "" Then
MsgBox "Creating pdf file." & vbCrLf & vbCrLf & _
"An error has occured: Time is up!", vbExclamation + vbSystemModal
End If
Exit_Word2PDF:
Set oPDF = Nothing
Set oWord = Nothing
Exit Function
EH_Word2PDF:
MsgBox "There was an error converting Word2PDF ->" & Error(err) & ", " & CStr(err)
Word2PDF = "Error"
Resume Exit_Word2PDF
End Function