Solved How to find OS default printer? (1 Viewer)

KitaYama

Well-known member
Local time
Today, 21:15
Joined
Jan 6, 2022
Messages
1,489
Is there any way to find the name of current default printer? (OS side not Access side)

I can set the default printer by :
Code:
Dim W As New WshNetwork
W.SetDefaultPrinter ("MyPrinterName")

But how I can retrieve the name of current default printer?

Thanks for any kind of advice.
 
Solution
Code:
'arnelgp
'taken from somewhere, i don't recall, sorry
'credit to the owner

Public Function getDefaultPrinter() As String
    Dim computer            As String
    Dim wmiService          As Object
    Dim installedPrinters   As Variant
    Dim printer             As Object
    Dim i                   As Integer
On Error Resume Next
    'Set the computer. Dot means the computer running the code.
    computer = "."
    
    'Get the WMI object
    Set wmiService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & computer & "\root\cimv2")
    
    'Retrieve information about the installed printers (by running a query).
    Set installedPrinters = wmiService.ExecQuery("Select * from Win32_Printer")
        
    'If an...

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 07:15
Joined
Feb 28, 2001
Messages
26,996
Here is a reference on the application printers. The Application object has a collection of Printers (printer objects) that can be enumerated using a standard "FOR EACH object IN collection" syntax.


This shows you how to use the WMI library to find the default printer through VBA.


Somewhere in these references you will find what you want.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:15
Joined
May 7, 2009
Messages
19,169
Code:
'arnelgp
'taken from somewhere, i don't recall, sorry
'credit to the owner

Public Function getDefaultPrinter() As String
    Dim computer            As String
    Dim wmiService          As Object
    Dim installedPrinters   As Variant
    Dim printer             As Object
    Dim i                   As Integer
On Error Resume Next
    'Set the computer. Dot means the computer running the code.
    computer = "."
    
    'Get the WMI object
    Set wmiService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & computer & "\root\cimv2")
    
    'Retrieve information about the installed printers (by running a query).
    Set installedPrinters = wmiService.ExecQuery("Select * from Win32_Printer")
        
    'If an error occurs in the previous step, inform the user.
    If Err.Number <> 0 Then
        MsgBox "Could not retrieve the printer information from WMI object!", vbCritical, "WMI Object Error"
        Exit Function
    End If
    
    'https://stackoverflow.com/questions/41026649/using-managementobjectsearcher-and-win32-printer-to-check-status
    'Printer status
    'Other (1)
    'Unknown (2)
    'Idle (3)
    'Printing (4)
    'Warmup (5)
    'Stopped Printing(6)
    'Offline (7)
    
    'Loop through all the installed printers and get their name. Check if one of them is the default one.
    For Each printer In installedPrinters
        'Write the results to the worksheet.
        'Debug.Print printer.NAME, printer.PrinterStatus, printer.Local
        If (printer.Default) Then
            getDefaultPrinter = printer.NAME
            'Exit For
        End If
    Next printer

End Function
 
Solution

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:15
Joined
May 7, 2009
Messages
19,169
As you can see it is not my code, it maybe from MyEngineeringworld + some other sites.
if it is useful, i collect and add to my library.
 

KitaYama

Well-known member
Local time
Today, 21:15
Joined
Jan 6, 2022
Messages
1,489
As you can see it is not my code, it maybe from MyEngineeringworld + some other sites.
if it is useful, i collect and add to my library.
True genius lies not in doing the extraordinary things.
But in doing ordinary things extraordinary well.

Lous H. Wilson

As said, What you do may be ordinary. But for sure is extraordinary well.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 12:15
Joined
Sep 12, 2006
Messages
15,613
One reason why it's better to iterate the printers collection, is that the printer name in windows is case sensitive, but in access name comparisons are not case sensitive. It also avoids a run time error trying to set the printer directly

this syntax is from memory, and may be slightly incorrect.
set printer = application.printers("Special Printer") will give a run time error if the desired printer does not exist with that capitalisation.

So if you want to set the printer to "Special Printer"

check all the printers until you find one matching the description "special printer", and us that as the printer. Now whether you could have printers called both "Special Printer" and "special printer" in Windows is a different question.
 

KitaYama

Well-known member
Local time
Today, 21:15
Joined
Jan 6, 2022
Messages
1,489
this syntax is from memory, and may be slightly incorrect.
set printer = application.printers("Special Printer") will give a run time error if the desired printer does not exist with that capitalisation.
Before sending a job to the printer, I need to be sure OS default printer has not changed.
application.Printer.DeviceName doesn't return OS default printer. But Access printer.

Thanks.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 12:15
Joined
Sep 12, 2006
Messages
15,613
That's not really what I said or meant
@arnelgp explained how to establish what the name of the default printer, but the default printer is the active printer unless you specify a different printer for access to use.

If you want a particular printer, then you can set the access printer as the active printer.
When you are finished set that access printer to nothing, and you return to the windows default.
 

KitaYama

Well-known member
Local time
Today, 21:15
Joined
Jan 6, 2022
Messages
1,489
If you want a particular printer, then you can set the access printer as the active printer.
When you are finished set that access printer to nothing, and you return to the windows default.
Seems that we have a little problem in communicating.
Active printer in Access is used for printing reports. While I'm not trying to print reports. I'm trying to print external files (pdf, Drawings, Cad, Nc & text files). And no matter what printer is set as Access Active printer, print job goes to OS default printer when I use a shell print.
That's why working with access printer doesn't cover my problem. Back to my first post, I need to work with OS side printer. Not Access.

Thanks for your input though.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 12:15
Joined
Sep 12, 2006
Messages
15,613
I see. I imagine the shell to the OS does use the system printer. I am not sure whether you can change the OS printer from inside access.

The access printer lets you manage the active printer for printing access reports.
 

KitaYama

Well-known member
Local time
Today, 21:15
Joined
Jan 6, 2022
Messages
1,489
I am not sure whether you can change the OS printer from inside access.
Yes you can. My first post above has the code to change OS default printer.
You need a Windows Script Host Object Model reference or late bind it.

Code:
Dim W As New WshNetwork
W.SetDefaultPrinter ("MyPrinterName")
 

Users who are viewing this thread

Top Bottom