How to set a printer to be the system default printer?
Hi all,
first of all: please be pateient with me as I'm not at all an experienced VB programmer.
Reading lots of articles on MSDN I learned how to use PrinterSettings and PrintDialog and control printer settings. What I didn't find, though, is a way to make one of the installed printers the system default printer using VB 2005 Express.
If - for some reason - that's not possible using VB 2005 Express, there's still a 2nd option; in the forums I found a link to a technote explaining how to set the default printer using VB 6.0 / through Win32 API calls (http://support.microsoft.com/default.aspx?scid=kb;en-us;266767). I'd like to avoid the hassle, but if necessary I would do it. Unfortunately, I don't know how to retrieve the CURRENT system default printer, so that I can switch back.. (I need to only temporarily change the default setting, for reasons that are similar to the ones mentioned in the said technote).
Any hint or help is highly appreciated.
Best regards,
-Lothar
[1003 byte] By [
lm089] at [2007-12-25]
Sounds good to me; and I'd be more than greatful if could give me an idea how to code this in VB.
Reading the example I have to relaize, though, that this is limited to WinXP an Win2003. In my case, however, I know that at least some of the users are running Win2000. But I found this here in "scripting guy": http://www.microsoft.com/technet/scriptcenter/resources/qanda/nov04/hey1117.mspx
I guess, once I learned how to code the script code in VB.NET I should be able to apply that knowledge to Win2000 environments...
Thanks in advance for helping
-Lothar
ok, lets give this a shot. Fingers crossed.....
Import the System.Management and System.Management.Instrumentation namespace
To get a list of all printers installed on the local system:
| |
Dim theManagementScope as new ManagementScope("\\" & Environment.MachineName & "\root\cimv2") Dim theQueryString as String = "SELECT * FROM Win32_Printer" Dim theObjectQuery as new ObjectQuery(theQueryString) Dim theSearcher as new ManagementObjectSearcher(theManagementScope, theObjectQuery) Dim theResults as ManagementObjectCollection = theSearcher.Get() for each currentResult As ManagementObject in theResults MessageBox.Show(currentResult("Name").ToString()) next
|
To SET a printer:
| |
Dim theManagementObject as new ManagementObject("\\" & Environment.MachineName & "\root\cimv2", "Win32_Printer.DeviceID='PrinterName'", nothing) Dim theOutputParameters as ManagementBaseObject = theManagementObject.InvokeMethod("SetDefaultPrinter", nothing, nothing) MessageBox.Show("Return Value: " & theOutputParameters("ReturnValue").ToString())
|
Return value should be 0 to indicate success
Does this help?
Hi, and thanks a lot
retrieving the list of installed printers worked fine; setting the default printer however didn't. The line
Dim theOutputParameters as ManagementBaseObject = theManagementObject.InvokeMethod("SetDefaultPrinter", nothing, nothing)throws a "Not found" Exception:
System.Management.ManagementException was unhandled
Message="Not found "
Source="System.Management"
This isn't too bad, though, cause meanwhile I found a quite simple way through the API, i.e. calling the SetDefaultPrinter function from winspool.dll:
http://www.pinvoke.net/default.aspx/winspool.SetDefaultPrinter
Still, querying WMI seems to be an interesting option, though I have to admit that I don't understand too much of your code; probably due to the very late hour (22:25 CEST).
Best regards,
-Lothar
No, it's a WinXP Pro SP2 machine...
Please be aware that I already found a solution going completely different ways (through win32 api ==> calling winspool.drv functions directly). So please don't dive too deep into this subject, as there's no more real priority. But of course I'd nevertheless be interested to see it work using the WMI database; you never know, someday I might need this as well 
many thanks anyways,
-Lothar