Setting the System Default Printer with Visual Basic 6
I know this doesn't exactly qualify as “.NET” knowledge but, it's something I've had to battle with for a while so I thought I would post it in case anyone else finds it as helpful as I have.
How to set which printer is the system default printer
Now, setting a printer to be the system default printer in Visual Basic 6 is way more difficult than it needs to be. If perhaps the DeviceName property of the VB.Printer object wasn't read-only we could set the default printer by passing the name of a printer to that property. Or, in perhaps a more OO approach, It would be nice if there was a “VB.DefaultPrinter” object that we could set equal to a particular VB.Printer object. Something like this would do:
Dim p as VB.Printer
For Each p In VB.Printers
If p.DeviceName = “ThePrinterIWantToBeTheDefault“ Then
Set VB.DefaultPrinter = p
End If
Next
I would at least feel better about writing code like that than having to monkey around with the Win.ini file. Sheesh.
UPDATE: Wow, I really feel stupid now. I spent the better part of an entire workday trying to figure out how to do this when I could have just referenced the Windows Script Host Object Model (/system32/wshom.ocx) and coded this:
Dim w As New WshNetwork
w.SetDefaultPrinter (”ThePrinterIWantToBeTheDefault”)
Set w = Nothing
wow, 3 lines. Much easier. Thank you WSH team.