How to send a job to a specific Printer? (1 Viewer)

KitaYama

Well-known member
Local time
Today, 13:53
Joined
Jan 6, 2022
Messages
1,490
This is Outlook VBA question.

The following code prints selected mails in current folder :
Code:
Public Sub test()
 
    Dim Mail As Outlook.MailItem

    Set Mail = Application.ActiveExplorer.Selection(1)
    Mail.PrintOut
 
End Sub

How can I send print job to a specific printer, not windows default printer?

Thanks for any kind of advice.
 
Last edited:

Eugene-LS

Registered User.
Local time
Today, 07:53
Joined
Dec 7, 2018
Messages
481
How can I send print job to a specific printer, not windows default printer?
Code:
Dim prtDefault As Printer

    Set Application.Printer = Application.Printers(0)

    Set prtDefault = Application.Printer

    With prtDefault
        MsgBox "Device name: " & .DeviceName & vbCrLf _
            & "Driver name: " & .DriverName & vbCrLf _
            & "Port: " & .Port, vbInformation, "Default Printer:"
    End With

    Set Application.Printer = Application.Printers(1) 'Enother printer
     With Application.Printer
        MsgBox "Device name: " & .DeviceName & vbCrLf _
            & "Driver name: " & .DriverName & vbCrLf _
            & "Port: " & .Port, vbInformation, "Application Printer:"
    End With

    'Printing ...

'Set back to Default
    Set Application.Printer = prtDefault
Note:
As it turned out: the code works only under MS Access
 
Last edited:

KitaYama

Well-known member
Local time
Today, 13:53
Joined
Jan 6, 2022
Messages
1,490
I may be wrong but the screenshot you've submitted is Access reports' page setup window.
As far as I know, it's not available in Outlook.

In case of the code you submitted, Outlook VBA doesn't support Printer Property for Application object.
Both following lines give me the following error:

Code:
Set Application.Printer = Application.Printers(0)
Set prtDefault = Application.Printer

2022-03-31_15-18-16.png


Thanks for trying to help.
 

KitaYama

Well-known member
Local time
Today, 13:53
Joined
Jan 6, 2022
Messages
1,490
Sorry, I did not notice that the question was about Outlook - the answer was about MS Aaccess
Let me see ...
@Eugene-LS Thanks for looking into this.
The only way I could send a job to a not default printer was the following. It makes several screen flickers and a lag time, possibly waiting for windows to change default printer. A simpler way is much appreciated.

Code:
Private Declare PtrSafe Function GetProfileStringA Lib "kernel32" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long

Public Sub PrintPdf()
    
    Dim Mail As Outlook.MailItem
    Dim DefaultPrinter As String
    Dim W As New WshNetwork
    Dim strLPT As String * 255
    
    Call GetProfileStringA("Windows", "Device", "", strLPT, 254)
    DefaultPrinter = Trim(strLPT)
    DefaultPrinter = Left(DefaultPrinter, InStr(DefaultPrinter, ",") - 1)
    
    W.SetDefaultPrinter ("Adobe PDF")
    Set Mail = Application.ActiveExplorer.Selection(1)
    Mail.PrintOut
    W.SetDefaultPrinter (DefaultPrinter)
    
End Sub
 

Eugene-LS

Registered User.
Local time
Today, 07:53
Joined
Dec 7, 2018
Messages
481
Both following lines give me the following error:
For use MS word to print:
Code:
Sub PrintEmail()
'Use a Specific Printer to Print Emails from MS OutLook - for Open EMail Item
'----------------------------------------------------------------------------------------------
    Dim objItem As Object
    Dim objMail As Outlook.MailItem
    Dim objWordApp As Object ' Word.Application
    Dim strTempFolder As String
    Dim strMailDocument As String
    Dim objMailDocument As Object ' Word.Document
    Dim strPrinter As String

On Error GoTo PrintEmail_Err

    Select Case Application.ActiveWindow.Class
           Case olInspector
                Set objItem = ActiveInspector.CurrentItem
           Case olExplorer
                Set objItem = ActiveExplorer.Selection.Item(1)
    End Select
 
    If TypeOf objItem Is MailItem Then
       Set objMail = objItem
 
       Set objWordApp = CreateObject("Word.Application")
       strTempFolder = CStr(Environ("USERPROFILE")) & "\AppData\Local\Temp"
       strMailDocument = strTempFolder & "\" & Format(Now, "yyyymmddssnn") & ".doc"
       objMail.SaveAs strMailDocument, olDoc
 
       Set objMailDocument = objWordApp.Documents.Open(strMailDocument)
       objWordApp.Visible = True
       objMailDocument.Activate
 
       strPrinter = objWordApp.ActivePrinter
      
        'Change to the name of specific printer
        'objWordApp.ActivePrinter = "Specific Printer"
        objWordApp.ActivePrinter = "Microsoft XPS Document Writer"

      
       objWordApp.PrintOut Range:=0, Item:=0 'wdPrintAllDocument=0 wdPrintDocumentContent=0
       objWordApp.ActivePrinter = strPrinter
 
       objMailDocument.Close False
       objWordApp.Quit
      
       Kill strMailDocument
    End If

PrintEmail_End:
    On Error Resume Next
    Set objMailDocument = Nothing
    Set objWordApp = Nothing
    Err.Clear
    Exit Sub

PrintEmail_Err:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in Sub" & _
           "PrintEmail - ThisOutlookSession.", vbCritical, "Error!"
    Debug.Print "PrintEmail_Line: " & Erl & "."
    Err.Clear
    Resume PrintEmail_End
End Sub
 

KitaYama

Well-known member
Local time
Today, 13:53
Joined
Jan 6, 2022
Messages
1,490
I'll give it a try as soon as I'm back to office.

Thank you.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 23:53
Joined
Feb 28, 2001
Messages
27,001
  1. Select File, then Print or click on the Print button at the top of the browser or application.
  2. Select the number of copies you want printed, pages and other details - click on OK.
  3. Documents will be forwarded to the Print Station.

The original question was based on using code. KitaYama was asking for a programming way around this. You offered a manual method.
 

Users who are viewing this thread

Top Bottom