• Print doc to PDF (2003)

    • This topic has 7 replies, 5 voices, and was last updated 19 years ago.
    Author
    Topic
    #432374

    Courtesy of Hans:

    Try using ShellExecute:

    Put the following declarations at the beginning of a module:

    Private Declare Function ShellExecute Lib “shell32.dll” Alias “ShellExecuteA” _
    (ByVal hwnd As Long, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) As Long

    Private Const SW_SHOWNORMAL = 1

    Public Sub PrintPDF(fn As String)
    Dim lngResult As Long
    lngResult = ShellExecute(0&, “Print”, fn, 0&, 0&, SW_SHOWNORMAL)
    If lngResult <= 32 Then
    MsgBox "Something went wrong.", vbExclamation
    End If
    End Sub

    To print test.pdf, use

    Call PrintPDF("test.pdf")

    or

    Dim fn As String
    fn = "test.pdf"
    Call PrintPDF(fn)

    Viewing 0 reply threads
    Author
    Replies
    • #1014112

      In Access I have written VBA to create a Word document based on a template. I use code to write to the Word document, and Save it.
      Now I would like to print the Saved document to a PDF file. Systems have Adobe Acrobat, version 7.
      Is there a way that I can use VBA to have Acrobat open the Word.doc and print to the Acrobat distiller?

      Here is the last line of the VBA for Word:

      ‘Save the document with the save name just created
      gappWord.ActiveDocument.SaveAs strSaveNamePath

      I thought I could add”
      gappWord.ActiveDocument.PrintOut( ?????)
      I need help with the syntax for PrintOut to include all pages to the PDF distiller which is not the default printer

      • #1014114

        OOoops – jumped the gun with my reply – however, I’m sure someone will help out. Sorry I had my upside-down glasses on.

      • #1014123

        Try recording a macro in Word of printing a document to PDF. This should give you the exact name of the PDF printer.
        You can then probably use something like

        Dim strOriginalPrinter As String
        strOriginalPrinter = gappWord.ActivePrinter
        gappWord.ActivePrinter = “Name of the PDF printer”
        gappWord.ActiveDocument.PrintOut
        gappWord.ActivePrinter = strOriginalPrinter

      • #1014133

        Of course Hans’ code works just fine. But there’s a little gotcha: it sets the system default printer to the chosen printer. So there’s a – admittedly slim – chance that another (background) application’s printout is also redirected to that printer. This can be avoided by inserting, right after the line strOriginalPrinter = gappWord.ActivePrinter:

        With Dialogs(wdDialogFilePrintSetup)
        .DoNotSetAsSysDefault = True
        .Execute
        End With

        Alternatively, this Dialog panel can be used to pick the desired (PDF) printer from the list of installed printers to make things more generic. This also eliminates the need to know the exact name of the printer to put it in the code. Simply use .Show instead of .Execute in the code above. In the panel, doubleclick the name of the printer and then click the Close button. So the whole Sub would look like:

        Sub PrintToOtherPrinter(strDocName As String)
        Dim strOriginalPrinter As String
        strOriginalPrinter = gappWord.ActivePrinter
        With Dialogs(wdDialogFilePrintSetup)
        .DoNotSetAsSysDefault = True
        .Show
        End With
        gappWord.ActiveDocument.PrintOut
        gappWord.ActivePrinter = strOriginalPrinter
        End Sub

      • #1014136

        If you have installed the Adobe PDFMaker macro then you can try
        Application.Run MacroName:="AdobePDFMakerA.AutoExec.ConvertToPDF"

        StuartR

        • #1014288

          Thanks much to HansV, JanB, StuartR, and lorraine.

          You have provided me with some good options to work with. And none looked like what I had been trying to work out, look nothing like I expected.

          I probably won’t get to test this out until the weekend (so there is still time for more suggestions), but I will return with my final decision as example for others.

          When I return I hope it will be with the final solution, not more questions.

          Thank you again…..

      • #1014511

        I have had a chance to look at PDFMaker macro suggestion. It apears that full Acrobat is needed vs. Reader. So this may be a cost prohibitive choice if stations do not have full Acrobat already..

        I have just tried another alternative approach that uses no additional programming, but does require an inexpensive utility..

        PDF995 allows me to create a PDF printer, it can also be shared on the server. (“995” is for the $10 pricetag). They have a link to OmniFormat.
        OmniFormat has a utility used with PDF995 that allows me to define folders, and document source types to be converted to PDF. The folder is monitored, as soon as a anyname.doc shows up in the folder, it is immediately converted into anyname.PDF and the anyname.doc file is deleted. You can also start to monitor an existing folder and all existing documents of the defined type(s) will be instantly converted, no need to do anything else.

        I have used PDF995 for a number of years, but never tried OmniFormat before this. The PDF printer available for all programs is a nice tool to have.

        Initial testing looks pretty good, but there will probably be some “gotcha”s. Downside is the need to install other software in addition to the database.
        By using VBA that will be eliminated.

    Viewing 0 reply threads
    Reply To: Print doc to PDF (2003)

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

    Your information: