By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,722 Members | 1,252 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,722 IT Pros & Developers. It's quick & easy.

Get Default printer WMI

P: n/a
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=impersonat e}!\\" & 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
Nov 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
"Willem" wrote:
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=impersonat e}!\\" & 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

Nov 19 '05 #2

P: n/a
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)

"Willem" <Wi****@discussions.microsoft.com> wrote in message
news:DF**********************************@microsof t.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=impersonat e}!\\" & 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
|
|
Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.