locked
Access 2010 print a pdf document to the local printer RRS feed

  • Question

  • I am using Access 2010 on Windows 7.

    I have a list of several pdf documents that I want to send to the local printer.  How is the best way to us VBA code to print a pdf document?  Thanks

    Tuesday, February 8, 2011 8:57 PM

Answers

  • I will shell out to Adobe reader and print the pdf's.  Then once all are printed I will terminate the Adobe reader.

    Thank you for all your help.


    "C:\Program Files (x86)\Adobe\Reader 9.0\Reader\AcroRd32.exe" /n /t test.pdf

    taskkill /f /im AcroRd32.exe

    • Proposed as answer by Bruce Song Thursday, February 17, 2011 2:46 AM
    • Marked as answer by Bessie Zhao Friday, February 18, 2011 10:08 AM
    Thursday, February 10, 2011 3:58 PM
  • "CNC_Matthew" wrote in message
    news:fa7fa26c-2eca-4bf6-8919-2998f047371a@communitybridge.codeplex.com...
    >I agree is seems that there's no Print verb associated with the filetypes.
    >
    > I have not edited or changed the context menus.  I checked several other
    > types and do not get a print in the menu for others either.
    >
     
    If other file types also have no Print menu option, then I suspect that this
    option has been disabled in your installation of Windows 7.  You do have a
    printer installed, yes?  And set as the default printer?  I could imagine -- 
    with no real knowledge to back it up -- that the Print menu option might not
    appear if you didn't have a printer installed.  But you said you can print
    from within the applications, so that doesn't seem likely to be the cause.
     
    I think this is a Windows 7 problem, and I don't know anything about Windows
    7.  May I suggest that you visit a forum devoted to Windows 7, and ask there
    why the Print option doesn't appear in the context menus?  Maybe someone
    there can answer that question, and that answer will solve your specific
    problem with printing via ShellExecute.
     
     

    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html
    • Proposed as answer by Bruce Song Thursday, February 17, 2011 2:46 AM
    • Marked as answer by Bessie Zhao Friday, February 18, 2011 10:08 AM
    Thursday, February 10, 2011 4:53 PM

All replies

  • To the default printer? I would probably use the ShellExecute API with the Print verb.
    -Tom. Microsoft Access MVP
    Wednesday, February 9, 2011 2:18 AM
  • I am not able to get the ShellExecute API to work.  Here is sample code I am using... 

    Thanks

    Declare Function GetDesktopWindow Lib "user32" () As Long

       
    Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
                       (ByVal hwnd As Long, _
                        ByVal lpszOp As String, _
                        ByVal lpszFile As String, _
                        ByVal lpszParams As String, _
                        ByVal LpszDir As String, _
                        ByVal FsShowCmd As Long) As Long


    Sub TestShellExecute()
        Dim sPDFDocument        As String
        Dim exit_code           As Long
        Dim hWndDesk            As Long

        'sPDFDocument = "C:\DUMP\TEST.txt"
        sPDFDocument = "C:\DUMP\TEST.PDF"
       
        If Dir(sPDFDocument) <> "" Then
                       
            hWndDesk = GetDesktopWindow()
           
            'exit_code = ShellExecute(hWndDesk, "Open", sPDFDocument, "", "", 0)    ' Opens TXT file ok, does not open pdf file
            exit_code = ShellExecute(hWndDesk, "Print", sPDFDocument, "", "", 0)    ' Does not print txt or pdf file
           
            Debug.Print exit_code
          
        End If


    End Sub

    Wednesday, February 9, 2011 4:16 PM
  • "CNC_Matthew" wrote in message
    news:e94bdd34-f21e-4744-b768-ef3801cc4cb0@communitybridge.codeplex.com...
    >I am not able to get the ShellExecute API to work.  Here is sample code I
    >am using...
    >
    > Thanks
    >
    > Declare Function GetDesktopWindow Lib "user32" () As Long
    >
    >
    > Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    >                   (ByVal hwnd As Long, _
    >                    ByVal lpszOp As String, _
    >                    ByVal lpszFile As String, _
    >                    ByVal lpszParams As String, _
    >                    ByVal LpszDir As String, _
    >                    ByVal FsShowCmd As Long) As Long
    >
    >
    > Sub TestShellExecute()
    >    Dim sPDFDocument        As String
    >    Dim exit_code           As Long
    >    Dim hWndDesk            As Long
    >
    >    'sPDFDocument = "C:\DUMP\TEST.txt"
    >    sPDFDocument = "C:\DUMP\TEST.PDF"
    >
    >    If Dir(sPDFDocument) <> "" Then
    >
    >        hWndDesk = GetDesktopWindow()
    >
    >        'exit_code = ShellExecute(hWndDesk, "Open", sPDFDocument, "", "",
    > 0)    ' Opens TXT file ok, does not open pdf file
    >        exit_code = ShellExecute(hWndDesk, "Print", sPDFDocument, "", "",
    > 0)    ' Does not print txt or pdf file
    >
    >        Debug.Print exit_code
    >
    >    End If
    >
    >
    > End Sub
    >
     I have tested this code successfully on my Vista/A2003 system:
    '
    '------ start of code ------
    Option Compare Database
    Option Explicit
     
    Private 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
     
    '***Error Codes***
    Private Const ERROR_SUCCESS = 32&
    Private Const ERROR_NO_ASSOC = 31&
    Private Const ERROR_OUT_OF_MEM = 0&
    Private Const ERROR_FILE_NOT_FOUND = 2&
    Private Const ERROR_PATH_NOT_FOUND = 3&
    Private Const ERROR_BAD_FORMAT = 11&
     Function fPrintFile(stFile As String)
        ' This function uses ShellExecute to print, rather than
       ' open, the file.
        Dim lRet As Long, varTaskID As Variant
       Dim stRet As String
        'First try ShellExecute
       lRet = apiShellExecute(hWndAccessApp, "print", _
               stFile, vbNullString, vbNullString, 0&)
        If lRet > ERROR_SUCCESS Then
           stRet = vbNullString
           lRet = -1
       Else
           Select Case lRet
               Case ERROR_NO_ASSOC:
                   stRet = "Error: No associated application.  Couldn't print!"
               Case ERROR_OUT_OF_MEM:
                   stRet = "Error: Out of Memory/Resources. Couldn't print!"
               Case ERROR_FILE_NOT_FOUND:
                   stRet = "Error: File not found.  Couldn't print!"
               Case ERROR_PATH_NOT_FOUND:
                   stRet = "Error: Path not found. Couldn't print!"
               Case ERROR_BAD_FORMAT:
                   stRet = "Error:  Bad File Format. Couldn't print!"
               Case Else:
           End Select
       End If
       fPrintFile = lRet & _
                   IIf(stRet = "", vbNullString, ", " & stRet)
    End Function
     
    '------ ernd of code ------
     

    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html
    Wednesday, February 9, 2011 5:16 PM
  • I am on windows  7.

    When I try this using either the txt or pdf document I get the following error.

    31, Error: No associated application.  Couldn't print!

    Wednesday, February 9, 2011 5:41 PM
  • "CNC_Matthew" wrote in message
    news:7576b575-b9b2-4876-953d-7e26e7fa23bc@communitybridge.codeplex.com...
    >I am on windows  7.
    >
    > When I try this using either the txt or pdf document I get the following
    > error.
    >
    > 31, Error: No associated application.  Couldn't print!
    >
     
    That seems to imply that there is no application associated with the .txt
    and .pdf file-extensions.  Is that conceivable?  If you double-click on a
    .txt file or a .pdf file, does the file open in an appopriate application
    (usually NotePad or Adobe/Adobe Reader, respectively)?
     

    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html
    Wednesday, February 9, 2011 5:52 PM
  • From dos or File Manager the file opens.  I used file manager, selected open with and set the pdf and txt file.   I even checked the registery and they are associated correctly.  I modified your code and changed the code from "Print" to "Open" and both the txt file and pdf file opened.  It just does not recognize what "print" is trying to tell it.  I do not know if there is a setting for what to print a document with as there is for what to open a file with.

    Thank you for your help

    Wednesday, February 9, 2011 6:12 PM
  • "CNC_Matthew" wrote in message
    news:15cbb9fe-d5e4-4a88-96c0-1c460819f6da@communitybridge.codeplex.com...
    > From dos or File Manager the file opens.  I used file manager, selected
    > open with and set the pdf and txt file.   I even checked the registery and
    > they are associated correctly.  I modified your code and changed the code
    > from "Print" to "Open" and both the txt file and pdf file opened.  It just
    > does not recognize what "print" is trying to tell it.  I do not know if
    > there is a setting for what to print a document with as there is for what
    > to open a file with.
    >
     
    If you right-click on the file and look at the popup menu, is there a Print
    option on it?
     
     

    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html
    Wednesday, February 9, 2011 6:43 PM
  • No.

    There is

    Open
    Edit
    Open With >
    Share with >
    Restore previous versions
    Send To >
    Cut
    Copy
    Create shortcut
    Delete
    Rename
    Properties

    Wednesday, February 9, 2011 7:36 PM
  • "CNC_Matthew" wrote in message
    news:61894fb7-2121-425c-8ec9-6122d4301054@communitybridge.codeplex.com...
    > No.
    >
    > There is
    >
    > Open
    > Edit
    > Open With >
    > Share with >
    > Restore previous versions
    > Send To >
    > Cut
    > Copy
    > Create shortcut
    > Delete
    > Rename
    > Properties
    >
     Hmm, that suggests that there's no Print verb associated with the filetypes,
    but I don't know why that would be.  Have you done anything to edit or
    restrict the context menus?  Do you get a Print menu item for other
    filetypes, such as .doc or .xls?  I'm not running Windows 7, so I have no
    idea if there's some specific configuration option related to this.
     
     

    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html
    Wednesday, February 9, 2011 9:10 PM
  • I agree is seems that there's no Print verb associated with the filetypes.

    I have not edited or changed the context menus.  I checked several other types and do not get a print in the menu for others either. 

    Thursday, February 10, 2011 12:40 PM
  • I think you may need to install a different PdfViewer program. There is the one from Adobe, and several others from 3rd parties.
    -Tom. Microsoft Access MVP
    Thursday, February 10, 2011 2:21 PM
  • I will shell out to Adobe reader and print the pdf's.  Then once all are printed I will terminate the Adobe reader.

    Thank you for all your help.


    "C:\Program Files (x86)\Adobe\Reader 9.0\Reader\AcroRd32.exe" /n /t test.pdf

    taskkill /f /im AcroRd32.exe

    • Proposed as answer by Bruce Song Thursday, February 17, 2011 2:46 AM
    • Marked as answer by Bessie Zhao Friday, February 18, 2011 10:08 AM
    Thursday, February 10, 2011 3:58 PM
  • "CNC_Matthew" wrote in message
    news:fa7fa26c-2eca-4bf6-8919-2998f047371a@communitybridge.codeplex.com...
    >I agree is seems that there's no Print verb associated with the filetypes.
    >
    > I have not edited or changed the context menus.  I checked several other
    > types and do not get a print in the menu for others either.
    >
     
    If other file types also have no Print menu option, then I suspect that this
    option has been disabled in your installation of Windows 7.  You do have a
    printer installed, yes?  And set as the default printer?  I could imagine -- 
    with no real knowledge to back it up -- that the Print menu option might not
    appear if you didn't have a printer installed.  But you said you can print
    from within the applications, so that doesn't seem likely to be the cause.
     
    I think this is a Windows 7 problem, and I don't know anything about Windows
    7.  May I suggest that you visit a forum devoted to Windows 7, and ask there
    why the Print option doesn't appear in the context menus?  Maybe someone
    there can answer that question, and that answer will solve your specific
    problem with printing via ShellExecute.
     
     

    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html
    • Proposed as answer by Bruce Song Thursday, February 17, 2011 2:46 AM
    • Marked as answer by Bessie Zhao Friday, February 18, 2011 10:08 AM
    Thursday, February 10, 2011 4:53 PM