Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to detect default windows printer name in VB?

Status
Not open for further replies.

19790203

Programmer
Oct 7, 2005
13
PL
simple question:

How to detect default windows printer name in VB?
 
Try this for maximum backward compatibility.
[tt]
Private Sub x_Click()

'reference: Microsoft WMI Scripting v1.1 Library
Dim sComputer As String
Dim svc As ISWbemServices
Dim cprt As ISWbemObjectSet
Dim oprt As ISWbemObject
Dim bExistDefault As Boolean
Dim sNameDefault As String
Dim sNameComp As String

'local : ".","127.0.0.1","localhost", computer name
'remote: ip address or computer name
sComputer = "."
bExistDefault = False
sNameDefault = ""

If (sComputer = ".") Or (sComputer = "127.0.0.1") Or (LCase(sComputer) = "localhost") Then
sNameComp = "Localhost"
Else
sNameComp = sComputer
End If

Set svc = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
Set cprt = svc.execquery("select Name, Attributes from win32_printer")

For Each oprt In cprt
If (oprt.Attributes And &H4) = &H4 Then
bExistDefault = True
sNameDefault = oprt.Name
Exit For
End If
Next

Set cprt = Nothing
Set svc = Nothing

If bExistDefault Then
MsgBox "Computer " & sNameComp & " has an assigned default printer : " & sNameDefault & "."
Else
MsgBox "Computer " & sNameComp & " does not have a default printer assigned."
End If

End Sub
[/tt]
 
>>for maximum backward compatibility

I am not sure what you mean by this but WMI does not ensure backward compatibility in the sense of W9x/NT4 machines.
 
>>for maximum backward compatibility
>I am not sure what you mean
I refer to detecting &H4 attribute flag. You have added method for later version. But, this check is compatible all along.
 
<ahem>

I think what bjd4jc is querying is how the WMI method (which won't by default work on admittedly obsolete W9x/NT4 machines) to check the &H4 attribute is more backwards compatible than VB6's Printer.Default method, which works on all platforms on which VB6 works
 
Okay, maybe the added functionality of remoting makes a difference in perspective.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top