none
Print to PDF in Word 2003

    Question

  • Hi,

    I'm using VBA to save a document to PDF format in Word 2003 (NOT 2007 or later).  This is my code, but it is producing an 'empty' file that Acrobat cannot read.

    ActivePrinter = "Adobe PDF"
    ActiveDocument.PrintOut False, , , CurDir & "\" & Replace(ThisDocument.Name, ".doc", "") & ".pdf"

    In order to get his to work I had to edit the Printer Properties and uncheck "Rely on system fonts only; do not use document fonts".  I'm not sure what effect that will have on printing other PDFs, but otherwise Acrobat would give me an error and not generate anything.

    Is there a better way to do this??  This seems the simplest, but it's not working, so I am open to any suggestions.

    Thanks,

     


    Brian
    Wednesday, February 09, 2011 9:18 PM

All replies

  • Hi Brian,

     

    Simply rename a .doc to .pdf doesn't do the trick.

    Would you be able to install a addin into your desktop? This solution might fix your problem, although will need you to download a third-party component.

    Credits: http://www.tek-tips.com/viewthread.cfm?qid=416162

    Adobe SDK: http://www.adobe.com/devnet/acrobat/downloads.html

    1. Print the range to a postscript file using Acrobat Distiller
    2. Convert postscript file to .PDF using Acrobat Distiller API.

    Make sure that you uncheck the "Do not send fonts to Distiller" option in the Distiller properties.  You do this by entering the Distiller properties>>General Tab>>Printing Preferences>>Adobe PDF Settings and uncheck the "Do not send fonts to Distiller" option.  You will get an error if you don't do this.

    In the Excel Visual Basic Editor, make sure you include a reference to Acrobat Distiller.

    Here is the code....

    *********************************************
    Private Sub CommandButton1_Click()

    ' Define the postscript and .pdf file names.
    Dim PSFileName as String
    Dim PDFFileName as String
    PSFileName = "c:\myPostScript.ps"
    PDFFileName = "c:\myPDF.pdf"

    ' Print the Excel range to the postscript file
    Dim MySheet As WorkSheet
    Set MySheet = ActiveSheet
    MySheet.Range("myRange").PrintOut copies:=1, preview:=False, ActivePrinter:="Acrobat Distiller", printtofile:=True, collate:=True, prttofilename:=PSFileName

    ' Convert the postscript file to .pdf
    Dim myPDF As PdfDistiller
    Set myPDF = New PdfDistiller
    myPDF.FileToPDF PSFileName, PDFFileName, ""

    End Sub


    Tiago Cardoso VB / VBA Analyst
    Thursday, February 10, 2011 12:19 PM
  • Hi Tiago,

    Thanks.  I modified your code to work for MS Word.  Here is what I have:

        Dim psFileName As String
        Dim pdfFileName As String
        psFileName = CurDir & "\" & Replace(ThisDocument.Name, ".doc", "") & ".ps"
        pdfFileName = CurDir & "\" & Replace(ThisDocument.Name, ".doc", "") & ".pdf"
       
        ActivePrinter = "Adobe PDF"
        ActiveDocument.PrintOut outputfilename:=psFileName, printtofile:=True
     
        Dim myPDF As PdfDistiller
        Set myPDF = New PdfDistiller
       
        myPDF.FileToPDF psFileName, pdfFileName, ""

     

    The .ps file is getting created, but in the .log file that is generated I get this:

    The process cannot access the file because it is being used by another process.

    \\FSRD001\Works\HQ\Data\OPERATIO\Data Systems Management\Custom Applications\Water Locates Report Toolbar\New Water Location Record Template.ps

     

    Any ideas as to how I can work around this problem.  How do I get Word to 'release' the .ps file so myPDF.FiletoPDF can use??

     


    Brian
    Thursday, February 10, 2011 3:32 PM