Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get default printer

Status
Not open for further replies.

tommeekers

Programmer
Sep 10, 2003
77
BE
I created a small VB script which runs at startup, and gets all available printers from the network. Next it deletes all current printers from my user's computer and reinstalls the up-to-date list. However, I need to find out which printer was set as the default, so that I could reset it after the script has been run.

I figured out a way to read the default printername from the registry, however I cannot read the drivername and printerport, which I both need to set the default printer registry key.

I someone could provide me with a solution, as simple as possible, I would be very gratefull.
 
Can you not just use the Printers collection? Each Printer has a DeviceName and DriverName property

For Each p in Printers
Debug.Print p.DriverName
Debug.Print p.DeviceName
Next p

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
I managed to put the printername, drivername and printerport into variables. How do I use these to reset the defaultprinter now ? I tried this code, but it doesn't seem to change anything. Do I need to edit the registry key directly ?
Code:
SetDefaultPrinter DefaultPrinterName, DefaultDriverName, DefaultPrinterPort
 
I tried editing the registry key directly. But somehow I can't get RegSetValue to work. I keep getting an error when I run following code.
Code:
Mainkey = "HKEY_CURRENT_USER"
SubKey = "Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"

If [i]DefaultPrinterCheck[/i] = True Then
	If RegOpenKeyEx(MainKey, SubKey, 0, KEY_SET_VALUE, hnd) Then
		RegSetValue(MainKey, SubKey, REG_SZ, [i]DefaultPrinter[/i])
		RegCloseKey hnd
	End If
End If
 
Seems like hard work to hack the registry. This will reset you default printer for the VB project. Don't forget to save your current default first so you can restore it when you're done.

Dim p As Printer
For Each p In Printers
If p.DeviceName = "\\EILEEN\HP LaserJet 4Si" Then
Set Printer = p
Exit For
End If
Next p

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top