Printing Word Documents to PDFCreator with VBA

Hi All,

I have several thousand word documents that I need to pdf and password protect. Links to the word docs exist in a Access database so I was hoping to be able to go through each database record, pick up the link, open it in PDFCreator, password protect the resulting PDF and save in a new location.

I found the following code on tinterweb which I call from the code that looks through the database records like so:

Call PrintToPDFCreator("test", newmfileurl, "???", "Library", "Test", True, False, True)

I can't figure out what to pass to the sub for the oDoc variable. No matter what I put in place of ????? it throws a type mismatch error. Can anyone help?

I'm using PDFCreator Free v3.2.2. Here's the tinterweb code.

Sub PrintToPDFCreator(sPDFName As String, _
sPDFPath As String, _
oDoc As Document, _
Optional sMasterPass As String, _
Optional sUserPass As String, _
Optional bNoCopy As Boolean, _
Optional bNoPrint As Boolean, _
Optional bNoEdit As Boolean)
Dim pdfjob As Object
Dim sPrinter As String
Dim iCopy As Integer, iPrint As Integer, iEdit As Integer

If bNoCopy Then iCopy = 1 Else iCopy = 0
If bNoPrint Then iPrint = 1 Else iPrint = 0
If bNoEdit Then iEdit = 1 Else iEdit = 0

'Change active printer to PDFCreator
With Dialogs(wdDialogFilePrintSetup)
    sPrinter = .Printer
    .Printer = "PDFCreator"
    .DoNotSetAsSysDefault = True
    .Execute
End With

Set pdfjob = CreateObject("PDFCreator.clsPDFCreator")

With pdfjob
    If .cStart("/NoProcessingAtStartup") = False Then
        GoTo err_Handler
    End If

    .cStart "/NoProcessingAtStartup"
    .cOption("UseAutosave") = 1
    .cOption("UseAutosaveDirectory") = 1
    .cOption("AutosaveDirectory") = sPDFPath
    .cOption("AutosaveFilename") = sPDFName
    .cOption("AutosaveFormat") = 0        ' 0 = PDF

    If Not sMasterPass = vbNullString Then

        'The following are required to set security of any kind
        .cOption("PDFUseSecurity") = 1
        .cOption("PDFOwnerPass") = 1
        .cOption("PDFOwnerPasswordString") = sMasterPass

        'To set individual security options
        .cOption("PDFDisallowCopy") = iCopy
        .cOption("PDFDisallowModifyContents") = iEdit
        .cOption("PDFDisallowPrinting") = iPrint

        'To force a user to enter a password before opening
        .cOption("PDFUserPass") = 1
        .cOption("PDFUserPasswordString") = sUserPass
        'To change to High encryption
        .cOption("PDFHighEncryption") = 1
    End If

    .cClearCache
End With

'Print the document to PDF
oDoc.PrintOut

'Wait until the print job has entered the print queue
Do Until pdfjob.cCountOfPrintjobs = 1
    DoEvents
Loop
pdfjob.cPrinterStop = False

'Wait until PDF creator is finished then release the objects
Do Until pdfjob.cCountOfPrintjobs = 0
    DoEvents
Loop
pdfjob.cClose
'Restore the original printer
With Dialogs(wdDialogFilePrintSetup)
    .Printer = sPrinter
    .Execute
End With

lbl_Exit:
Set pdfjob = Nothing
Exit Sub
err_Handler:
MsgBox "Unable to initialize PDFCreator." & vbCr & vbCr & _
"This may be an indication that the PDF application has become corrupted, " & _
"or its spooler blocked by AV software." & vbCr & vbCr & _
"Re-installing PDF Creator may restore normal working."
Resume lbl_Exit
End Sub

Hi Retro67.
I have excatly the same problem with the "Printing Word Documents To PDF Creator with VBA" issue.

Did you find a way to solve it since?
Thank you

Hi,

the code snippet above can only work for PDFCreator 1.7.3 or lower, which is outdated by several years and no longer officially supported by us. But generally, it seems like it requires an actual document object as input which is documented here:
https://docs.microsoft.com/en-us/office/vba/api/word.document
If possible I strongly recommend using an up to date PDFCreator version, they all come with VBA examples which should help to get you started.

Best regards

Robin