View Single Post
 
Old 02-25-2015, 11:40 PM
gmayor's Avatar
gmayor gmayor is offline Windows 7 64bit Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Word 2003 does not have the ability to save to PDF. That was introduced with Word 2007.

If you want to batch save as PDF with Word 2003, you will need a third party PDF tool to create the PDFs, that can be supplied with a filename from code. That cuts the choices somewhat.

One that does work is PDFCreator, though it comes with a lot of unwanted baggage, so install only the PDFCreator part. I would also recommend installing version 1.7.3 rather than the newer version 2.0 which has even more bloat. Even that version can sometimes be a bit flaky when called from VBA.

You will then need a function to create the PDF from your macro. The following has not been tested with Word 2003, but should work

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."
    GoTo lbl_Exit
End Sub
Then replace the line
Code:
ActiveDocument.ExportAsFixedFormat OutputFileName:=strDocName, ExportFormat:=wdExportFormatPDF
with
Code:
PrintToPDFCreator strDocName, tFolder, ActiveDocument
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote