Results 1 to 15 of 15
  1. #1
    cebrower is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2018
    Location
    Spring Lake, MI
    Posts
    110

    Send a file to a printer that is not the default printer

    I need to print a file located on a network share. The file is in PDF format. The following code sends the file to the user's default printer:



    Dim Ltr As String
    Ltr = "e:\Folder Name\File Name.pdf"
    CreateObject("Shell.Application").NameSpace(0).Par seName(Ltr).InvokeVerb ("Print")

    This works fine, but I also need to sent the file to a "printer" that creates a PDF if the user checks a certain checkbox before clicking the Print button. This printer is named PDF Writer and every user has a copy in Devices and Printers. Any suggestions?

  2. #2
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,112
    Maybe this would help...https://www.ozgrid.com/forum/index.p...cific-printer/

    If not you can store the name of the current printer in a variable, then set the default printer to your virtual one (the PDF Writer) print the file using your code and finally reset the default printer to the original (see https://answers.microsoft.com/en-us/...7-7f622e2cfed2)

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  3. #3
    cebrower is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2018
    Location
    Spring Lake, MI
    Posts
    110
    I tried this but the file still goes to my HP laser printer. It's like the code did not actually change the PDF printer to be the default printer.

    Dim prt As Printer
    ' Get current default printer
    Set prt = Application.Printer
    ' Set default printer
    Application.Printer = Application.Printers("PDF Writer")
    ' Print something
    CreateObject("Shell.Application").NameSpace(0).Par seName(Ltr).InvokeVerb ("Print")
    ' Restore original printer
    Set Application.Printer = prt

  4. #4
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,112
    Can you try using Set :
    Code:
    Dim prt As Printer
    ' Get current default printer
    Set prt = Application.Printer
    ' Set default printer
    SET Application.Printer = Application.Printers("PDF Writer")
    ' Print something
    CreateObject("Shell.Application").NameSpace(0).Par seName(Ltr).InvokeVerb ("Print")
    ' Restore original printer
    Set Application.Printer = prt
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  5. #5
    cebrower is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2018
    Location
    Spring Lake, MI
    Posts
    110
    I tried adding Set. Still goes to the laser. I also tried changing to a different laser printer but I get an Invalid Call error. These are all network printers except the PDF Writer which is a virtual printer. I tried specifying the path to a different printer (\\htfs01\printer name where htfs01 is our print server) with and without quotes but I get the invalid call error.

  6. #6
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,404
    You can use this to show all the printer names known to the host PC.
    Code:
    Option Compare Database
    Option Explicit
    Sub ShowPrinters()
        Dim strMsg As String
        Dim prt As Printer
        strMsg = "Printers installed: " & Printers.Count & vbCrLf & vbCrLf & vbCrLf
        Select Case Printers.Count > 0
            Case True
                For Each prt In Application.Printers
                    strMsg = strMsg _
                    & "Device name: " & Chr$(34) & prt.DeviceName & Chr$(34) & vbCrLf _
                    & "Port is: " & prt.Port & vbCrLf & vbCrLf
                Next prt
            Case False
                strMsg = "No printers found"
        End Select
        MsgBox strMsg, vbOKOnly + vbInformation, " Discovered Printers "
    End Sub

  7. #7
    cebrower is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2018
    Location
    Spring Lake, MI
    Posts
    110
    I have another virtual PDF printer located on our file and print server. Its address is \\htfs01\NovaPDF. I modified my code as follows, but now I get the error message Run-time error '5': Invalid procedure call or argument at the line marked with '********. Any help would be appreciated.

    If Forms!frmOrderEntry!Type = "Quote PDF" Then
    Dim prt As Printer
    Dim prt2 As String
    ' Get current default printer
    Set prt = Application.Printer
    ' Set new default printer
    prt2 = "\\htfs01\novapdf"
    Set Application.Printer = Application.Printers(prt2) '****************
    ' Print something
    CreateObject("Shell.Application").NameSpace(0).Par seName(Ltr).InvokeVerb ("Print")
    ' Restore original printer
    Set Application.Printer = prt
    Else
    CreateObject("Shell.Application").NameSpace(0).Par seName(Ltr).InvokeVerb ("Print")
    End If

  8. #8
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,404
    I think for that to work, the printer must be available on the client PC, and be visible as indicated in post #6 before it can be assigned.

  9. #9
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,112
    See if the info in this link helps, I think it is closer to what you're trying to do:

    https://myengineeringworld.net/2018/...inter-vba.html

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  10. #10
    cebrower is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2018
    Location
    Spring Lake, MI
    Posts
    110
    The printer named PDF Writer is installed locally on my computer and on every user's computer on our network. It's my understanding that the following line of code will set the printer referenced in parentheses as the default printer. It is also my understanding that I can refer to the printer by number or by name.

    Set Application.Printer = Application.Printers(16)
    or
    Set Application.Printer = Application.Printers(PDF Writer)

    If I refer by number the file still goes to my laser printer. If I refer by name I get an error message when my cursor leaves that line of code. I've tried it by name as shown above, enclosed in quotes, enclosed in single quotes, by assigning it to a variable declared as a string and as a variable declared as a printer. Nothing works.

    I can't actually refer by number because the number might not be the same on each user's computer. I just tried it to see if it worked, which it doesn't. The name is the same on every user's computer and the printer is installed locally.

  11. #11
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,404
    Try this. After the report is opened in print preview:
    Code:
    Screen.ActiveReport.Printer = Application.Printers("PDF Writer")
    Edit:
    You might also try your code without the SET

  12. #12
    cebrower is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2018
    Location
    Spring Lake, MI
    Posts
    110
    Thanks for the suggestion davegri. The problem is that I'm not trying to print an Access report. I'm trying to create a pdf of a file that is in PDF format and that resides in a network share. For example, send e:\foldername\filename.pdf to the virtual printer named PDF Writer.

  13. #13
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,112
    Quote Originally Posted by cebrower View Post
    Thanks for the suggestion davegri. The problem is that I'm not trying to print an Access report. I'm trying to create a pdf of a file that is in PDF format and that resides in a network share. For example, send e:\foldername\filename.pdf to the virtual printer named PDF Writer.
    That is why I suggested to have a look at the link I've sent you yesterday that explains how to set the default Windows printer from VBA.

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  14. #14
    cebrower is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2018
    Location
    Spring Lake, MI
    Posts
    110
    Thanks much - the functions in that link solved the issue of changing the default printer. However, the file still goes to my laser printer instead of to the PDF Writer. This is the line that sends the print job. Ltr is a variable containing the path and filename that I'm trying to send to the PDF Writer.

    CreateObject("Shell.Application").NameSpace(0).Par seName(Ltr).InvokeVerb ("Print")

  15. #15
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,112
    See if using this alternate way to print helps you:
    From https://stackoverflow.com/questions/...nt-not-working
    Code:
    Option Explicit
    
    PublicDeclare PtrSafe Function ShellExecute Lib"shell32.dll"Alias"ShellExecuteW"(ByVal hWnd AsLong, _
                                                                        ByVal lpOperation As LongPtr, _
                                                                        ByVal lpFile As LongPtr, _
                                                                        ByVal lpParameters As LongPtr, _
                                                                        ByVal lpDirectory As LongPtr, _
                                                                        ByVal nShowCmd AsLong)AsLong
    
    
    PublicConst SW_HIDE AsLong=0
    'Hides the window and activates another window.
    
    PublicConst SW_MAXIMIZE  AsLong=3
    'Maximizes the specified window.
    
    PublicConst SW_MINIMIZE  AsLong=6
    'Minimizes the specified window and activates the next top-level window in the z-order.
    
    PublicConst SW_RESTORE  AsLong=9
    'Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when restoring a minimized window.
    
    PublicConst SW_SHOW  AsLong=5
    'Activates the window and displays it in its current size and position.
    
    PublicConst SW_SHOWDEFAULT  AsLong=10
    'Sets the show state based on the SW_ flag specified in the STARTUPINFO structure passed to the CreateProcess function by the program that started the application. An application should call ShowWindow with this flag to set the initial show state of its main window.
    
    PublicConst SW_SHOWMAXIMIZED  AsLong=3
    'Activates the window and displays it as a maximized window.
    
    PublicConst SW_SHOWMINIMIZED  AsLong=2
    'Activates the window and displays it as a minimized window.
    
    PublicConst SW_SHOWMINNOACTIVE  AsLong=7
    'Displays the window as a minimized window. The active window remains active.
    
    PublicConst SW_SHOWNA  AsLong=8
    'Displays the window in its current state. The active window remains active.
    
    PublicConst SW_SHOWNOACTIVATE  AsLong=4
    'Displays a window in its most recent size and position. The active window remains active.
    
    PublicConst SW_SHOWNORMAL  AsLong=1
    'Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when displaying the window for the first time.
    
    PublicEnum shexActions
        shexEDIT
        shexEXPLORE
        shexFIND
        shexOPEN
        shexPRINT
    EndEnum
    
    PrivateFunction getShellAction(ByRef enACTION As shexActions)AsString
        SelectCase enACTION
            Case shexActions.shexEDIT
                getShellAction ="EDIT"
            Case shexActions.shexEXPLORE
                getShellAction ="EXPLORE"
            Case shexActions.shexFIND
                getShellAction ="FIND"
            Case shexActions.shexOPEN
                getShellAction ="OPEN"
            Case shexActions.shexprint
                getShellAction ="PRINT"
        EndSelect
    EndFunction
    
    
    
    PublicFunction ShellEx(ByRef strFILE AsString, _
                            OptionalByRef lngWINDOWHANDLE AsLong=0, _
                            OptionalByRef shexACTION As shexActions =(-1), _
                            OptionalByRef strPARAMETERS AsString, _
                            OptionalByRef strDIRECTORY AsString, _
                            OptionalByRef lngSHOWCOMMAND AsLong=0)AsLong
    
        Dim lngReturnCheck AsLong
    
        lngReturnCheck =(-1)
    
        lngReturnCheck = ShellExecute(hWnd:=lngWINDOWHANDLE, lpOperation:=StrPtr(getShellAction(shexACTION)), lpFile:=StrPtr(strFILE), lpParameters:=StrPtr(strPARAMETERS), lpDirectory:=StrPtr(strDIRECTORY), nShowCmd:=lngSHOWCOMMAND)
    
            While lngReturnCheck =(-1)
                DoEvents
            Wend
    
        ShellEx = lngReturnCheck
    EndFunction
    
    Sub printBAR()
        Dim shFIcol         As Shell32.FolderItems
        Dim shFIx           As Shell32.FolderItem
        Dim shFLDx          As Shell32.Folder
        Dim lngX            AsLong
    
        Set shFLDx = GetFolder("Choose a folder...",True)
    
        Set shFIcol = shFLDx.Items()
    
        ForEach shFIx In shFIcol
                lngX = ShellEx(shFIx.Path,, shexPRINT)
                Debug.Print lngX
        Next
    EndSub
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Send file to Zebra Printer ZP 505
    By Ira in forum Programming
    Replies: 1
    Last Post: 08-11-2018, 06:50 AM
  2. printer default to pdf
    By wolfm in forum Programming
    Replies: 3
    Last Post: 02-07-2018, 12:59 PM
  3. Set Default Printer
    By jo15765 in forum Modules
    Replies: 5
    Last Post: 02-18-2016, 07:05 AM
  4. Replies: 3
    Last Post: 02-25-2014, 11:46 AM
  5. Print to Default Printer
    By bsc0617 in forum Access
    Replies: 7
    Last Post: 10-10-2013, 07:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums