Announcement

Collapse
No announcement yet.

Printing a file using VBA code

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Printing a file using VBA code

    Using the VBA code, I like to print (on the default printer) an existing file called "XYZ.pdf" that resides in the same folder as the Excel file. What would be VBA code for it?

    Important: I am not trying to make a pdf file out of an excel sheet/range. All I want is a VBA macro that prints a specific, existing pdf file (in the same folder as the excel file) to my default printer to print the entire pdf file on paper.
    Computer OS: Windows 7
    Excel Version: Office 2007

  • #2
    Re: Printing a file using VBA code

    Take a look at:
    Code:
    Option Explicit
     
    Declare Function apiShellExecute 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
     
    Public Sub PrintFile(ByVal strPathAndFilename As String)
     
        Call apiShellExecute(Application.hwnd, "print", strPathAndFilename, vbNullString, vbNullString, 0)
     
    End Sub
     
    Sub Test()
    
        PrintFile ("C:\Test.pdf")
    
    End Sub

    Comment


    • #3
      Re: Printing a file using VBA code

      Excel 2003, Windows XP.

      Working nicely for me

      Thank you for sharing.

      Yura

      Comment


      • #4
        Re: Printing a file using VBA code

        Thank you very much for your help, works perfect.

        Comment


        • #5
          Re: Printing a file using VBA code

          Hi
          Is there anyway you can print to another non-default printer using the above code in windows 7 excel 2010?
          Thanks / Jag

          Comment

          Working...
          X