Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Feb 2009
    Posts
    9
    Thanks
    5
    Thanked 0 Times in 0 Posts

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

    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.

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

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

    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:

    <code>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</code>

    Note that I do not have Acrobat, so this is "air code" based on documentation from the web. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

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

  3. #3
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

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

    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

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

    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.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  4. #4
    New Lounger
    Join Date
    Feb 2009
    Posts
    9
    Thanks
    5
    Thanked 0 Times in 0 Posts

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

    Thanks for the suggestions

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •