Get Default printer WMI

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
 
G

Guest

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
 
B

bruce barker

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
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top