• Code to Print Document as PDF in a Desired Folder (2007)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Code to Print Document as PDF in a Desired Folder (2007)

    Author
    Topic
    #457368

    I’m trying to write a macro that will that will print the current document that’s open to pdf file in a desired folder (in my case the folder is drive z:word).

    I’m using Word 2007 with Adobe Pro v8.

    In doing some research, it seems that I need to write a VBA code to accomplish the task. I have a very limited knowledge base when it comes to writing code. Here’s what I’ve come up with so far.

    Sub PrintPDF()

    ‘ PrintPDF Macro


    Application.PrintOut FileName:=””, Range:=wdPrintAllDocument, Item:= _
    wdPrintDocumentContent, Copies:=1, Pages:=””, PageType:=wdPrintAllPages, _
    ManualDuplexPrint:=False, Collate:=True, Background:=True, PrintToFile:= _
    False, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
    PrintZoomPaperHeight:=0

    Sub OpenFolder()
    With Application.Dialogs(wdDialogFileOpen)
    .Name = “Z:Word”
    .Show
    End With
    End Sub

    The code is not working correctly. Any help or suggestions are appreciated.

    Viewing 1 reply thread
    Author
    Replies
    • #1146556

      I don’t think Application.PrintOut will let you control what happens to the file after Word passes it to the print driver.

      Adobe does have an automation interface, which lets you convert a saved PostScript print job to PDF. So, for example, you could:

      (1) Change the printer to a PostScript printer (save and then set ActivePrinter)
      (2) Print to disk using Application.PrintOut, creating a file such as c:tempstufftemp.ps
      (3) Restore original printer
      (4) Automate the Distiller to convert temp.ps to a PDF file in the desired location

      For step 4, I think you would use something along the following lines:

      Dim pdf As PdfDistiller ' requires a reference to be set under Tools>References
      Dim intReturn as Integer
      intReturn = pdf.FileToPdf("c:tempstufftemp.ps", "z:wordnewPDF.pdf", "")
      Select Case intReturn
      Case 0
      MsgBox "Invalid parameters"
      Case -1
      MsgBox "File creation failed"
      Case Else
      MsgBox "It worked"
      End Select

      Note that I do not have Acrobat, so this is “air code” based on documentation from the web. grin

      Before going down that path, I suggest manually printing to a .ps file and see whether the Distiller can convert it successfully.

    • #1146607

      Rather than using Acrobat, I would suggest that you investigate the free Microsoft plugin which creates PDFs in all the Office 2007 applications. http://www.microsoft.com/downloads/details…&displaylang=en%5B/url%5D

      Assuming you have installed this plugin then the important line of code is as simple as
      ActiveDocument.SaveAs FileName:="D:Text.pdf", FileFormat:=wdFormatPDF

      You will need to add a few lines to assign the correct filename.

      You can still use Acrobat to edit existing PDFs but you no longer need to buy it just to create these files.

    Viewing 1 reply thread
    Reply To: Code to Print Document as PDF in a Desired Folder (2007)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: