Get Default printer WMI

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I'm trying to get the default printer using WMI.

I use the following code:

Private Function GetDefaultPrinter() As String
Dim objWMIService As Object
Dim colInstalledPrinters As Object
Dim objPrinter As Object
Dim strComputer As String
Dim strPrinter As String

strComputer = "MyComputerName"
objWMIService =
GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer &
"\root\cimv2")
colInstalledPrinters = objWMIService.ExecQuery("Select * from
Win32_Printer Where Default = True")
For Each objPrinter In colInstalledPrinters
strPrinter = objPrinter.Name()
Next
GetDefaultPrinter = strPrinter
End Function

I've a printer installed as default printer, but the function returns to
printer. What is going wrong?

Many thanks in advance.

Willem
 
Extra Info

Private Function GetDefaultPrinter2() As String
Dim oShell As Object
Dim sRegVal As String
Dim sDefault As String

oShell = CreateObject("WScript.Shell")
sRegVal = _
"HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"
sDefault = ""
On Error Resume Next
sDefault = oShell.RegRead(sRegVal)
sDefault = Left(sDefault, InStr(sDefault, ",") - 1)
On Error GoTo 0
GetDefaultPrinter2 = sDefault
End Function

Is not working either. When I look in the registry I find a string, sDefault
stays empty.

I'm using Windows XP.

Willem
 
asp.net runs as a service, and has no profile, thus no default printer.
specify the default printer name in the web config.

-- bruce (sqlwork.com)



| Hi,
|
| I'm trying to get the default printer using WMI.
|
| I use the following code:
|
| Private Function GetDefaultPrinter() As String
| Dim objWMIService As Object
| Dim colInstalledPrinters As Object
| Dim objPrinter As Object
| Dim strComputer As String
| Dim strPrinter As String
|
| strComputer = "MyComputerName"
| objWMIService =
| GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer &
| "\root\cimv2")
| colInstalledPrinters = objWMIService.ExecQuery("Select * from
| Win32_Printer Where Default = True")
| For Each objPrinter In colInstalledPrinters
| strPrinter = objPrinter.Name()
| Next
| GetDefaultPrinter = strPrinter
| End Function
|
| I've a printer installed as default printer, but the function returns to
| printer. What is going wrong?
|
| Many thanks in advance.
|
| Willem
|
|
 
Back
Top