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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Switching Printer Script

Status
Not open for further replies.

bitwizard2

Programmer
Sep 26, 2003
4
US
Here's the deal. Our office has a computer that needs to switch back and forth between a local card printer (makes ID cards) and a network laser printer. The card printer is physically attached to LPT1. And because the computer uses an old DOS program, the network printer has to be mapped to LPT1 when that DOS program tries to print. Hence, the need to switch... whichever printer is needed has to be at LPT1 and the other one has to be set to print to a file.

Manually this can be done by opening the Windows Printers folder by the following:
-Switching from card printer to network printer:
--Change card printer from LPT1 to "print to file".
--Capture Port LPT1 to the shared laser printer address
--Change laser printer from "print to file" to LPT1.

-Switching from network printer to card printer:
--Change laser printer from LPT1 to "print to file".
--End capture on LPT1
--Change card printer from "print to file" to LPT1.

Easy enough - so I thought I could write a windows script to make the change in one click.

Here's what I have:
***Start Code***
Set WshShell = WScript.CreateObject("WScript.Shell")

If WshShell.RegRead("HKLM\System\CurrentControlSet\Control\Print\Printers\Eltron P420 Card Printer\Port") = "FILE:" Then
'Switching to Card Printer

WshShell.RegWrite "HKLM\System\CurrentControlSet\Control\Print\Printers\Eltron P420 Card Printer\Port", "LPT1:"
WshShell.RegWrite "HKLM\System\CurrentControlSet\Control\Print\Printers\HP LaserJet 1100\Port", "FILE:"

WshShell.RegWrite "HKLM\System\CurrentControlSet\Control\Print\Printers\Eltron P420 Card Printer\Attributes", &H51
WshShell.RegWrite "HKLM\System\CurrentControlSet\Control\Print\Printers\HP LaserJet 1100\Attributes", &H41

WshShell.RegDelete "HKEY_USERS\.DEFAULT\Network\Persistent\LPT1\ProviderName"
WshShell.RegDelete "HKEY_USERS\.DEFAULT\Network\Persistent\LPT1\RemotePath"
WshShell.RegDelete "HKEY_USERS\.DEFAULT\Network\Persistent\LPT1\UserName"

WScript.Echo "Now set to print to the Card Printer."

Else
'Switching to Laser Printer

WshShell.RegWrite "HKLM\System\CurrentControlSet\Control\Print\Printers\Eltron P420 Card Printer\Port", "FILE:"
WshShell.RegWrite "HKLM\System\CurrentControlSet\Control\Print\Printers\HP LaserJet 1100\Port", "LPT1:"

WshShell.RegWrite "HKLM\System\CurrentControlSet\Control\Print\Printers\Eltron P420 Card Printer\Attributes", &H41
WshShell.RegWrite "HKLM\System\CurrentControlSet\Control\Print\Printers\HP LaserJet 1100\Attributes", &H51

WshShell.RegWrite "HKEY_USERS\.DEFAULT\Network\Persistent\LPT1\", ProviderName, "REG_SZ"
WshShell.RegWrite "HKEY_USERS\.DEFAULT\Network\Persistent\LPT1\ProviderName", "Microsoft Network"
WshShell.RegWrite "HKEY_USERS\.DEFAULT\Network\Persistent\LPT1\", RemotePath, "REG_SZ"
WshShell.RegWrite "HKEY_USERS\.DEFAULT\Network\Persistent\LPT1\RemotePath", "\\BILLING\HP"
WshShell.RegWrite "HKEY_USERS\.DEFAULT\Network\Persistent\LPT1\", UserName, "REG_SZ"
WshShell.RegWrite "HKEY_USERS\.DEFAULT\Network\Persistent\LPT1\UserName", "OFFICE"

WScript.Echo "Now set to print to the Laser Printer."
End If
***End Code***

Problem is that when this code changes the attributes in the registry (the WshShell.RegWrite lines that end with &H51 or &H41) it changes the type, too. Windows keeps the attributes as a DWORD hex value and when I run the code it changes to a string. What's the fix to that?

Any help is much appreciated!
David
 
I'm pretty sure this is happening because you are not specifying all of the parameters in your RegWrite calls. When using the RegWrite method follow the syntax below:

objShell.RegWrite(strName, anyValue, [strType])

You have to specify the last parameter [strType], which you did in some lines, but not in others. Use the following parameters based on the value type you want:

* String - REG_SZ
* Expandable String - REG_EXPAND_SZ
* Number - REG_DWORD
* Binary Value - REG_BINARY

So if you want to set your values as DWORD values than write your lines like this:

WshShell.RegWrite "HKLM\System\CurrentControlSet\Control\Print\Printers\Eltron P420 Card Printer\Attributes", &H41, REG_DWORD

Hope this helps :)

-= j@ckle =-
 
Use this, the registry stuff is overkill

Set WshNetwork = WScript.CreateObject("WScript.Network")
PrinterPath = "\\printservername\printersharename"
'WshNetwork.AddWindowsPrinterConnection PrinterPath
WshNetwork.SetDefaultPrinter PrinterPath

You can alos use local LPT printers as well. let me know if it helps.
 
Thanks guys! I'll give both a shot when I'm in the office on Tuesday and let you know how it goes.

David
 
j4ckl3, the 3rd parameter of the RegWrite method must be a string:
WshShell.RegWrite "HKLM\System\CurrentControlSet\Control\Print\Printers\Eltron P420 Card Printer\Attributes", &H41, "REG_DWORD"
 
Results:
j4ckl3's post - That certainly helped the problem (with PHV's correction) of writing the right type of data to the registry. However, there must be some other registry keys lurking that need to be changed to make the switch. Even when all those keys are properly changed the <control panel\printers> screen doesn't reflect it. And when I tried to print to the card printer, it wouldn't go.

So then I tried spazman's suggestion. Problem there is that there isn't a method (that I know of) to add a local printer. I can delete LPT1 when I want to print to the network laser printer, but I can't add LPT1 when I want to print to the local card printer.

You have all been helpful, but I guess my problem isn't solved, yet.

Thanks,
David
 
jr1000 posted ths example
WshNetwork.AddPrinterConnection &quot;LPT1:&quot;, &quot;\\server\printer1&quot;
not sure if it helps, but also suggested by spazman

regards
vonmoyla
 
WshNetwork.RemovePrinterConnection &quot;PRINTERNAME&quot;
 
Same way a remote printer I think WshNetwork.AddPrinterConnection &quot;LPT1:&quot;, &quot;DRIVERNAME&quot;
 
Here's a script to add a local printer, if it helps. In this case it's generic text, but could be any model.
The port code can be left out.

Function AddLocalPrinter

Dim oPort
Dim oMaster
Dim oPrinter


On Error Resume Next

Set oMaster = CreateObject(&quot;PrintMaster.PrintMaster.1&quot;)


For Each oPrinter In oMaster.Printers
'Enumerate all printers on Workstation's Printer Collection Set
If UCase(Left(Trim(oPrinter.PrinterName), 7) = &quot;Generic&quot;) Then
Set oMaster = Nothing
Exit Function
End If
Next

'Add port

Set oPort = CreateObject(&quot;Port.Port.1&quot;)
oPort.PortName = &quot;C:\temp\printfile.txt&quot;
oPort.PortType = 3
'- 1 = RAW
'- 2 = TCPLPR
'- 3 = Local
'- 4 = localDownLevel
'- 5 = LPRMON
'- 7 = HPDLC
'- 8 = Unknow

oMaster.PortAdd oPort

Set oPrinter = CreateObject(&quot;Printer.Printer.1&quot;)

oPrinter.PrinterName = &quot;Generic / Text Only&quot; ' name of the printer as it appears in the Printers folder

oPrinter.DriverName = &quot;Generic / Text Only&quot; ' name that is referenced in ntprint.inf

oPrinter.PortName = &quot;C:\temp\printfile.txt&quot; ' Specify a port name. Can also point to LPT or COM port.
oPrinter.ShareName = &quot;GenericT&quot;
oPrinter.Shared = True

oMaster.PrinterAdd oPrinter
oMaster.PrinterSet oPrinter

Set oPort = Nothing
Set oMaster = Nothing
Set oPrinter = Nothing

End Function
 
Hope this helps, this thread helped me fix this little script which i needed to make a user use a local printer as default printer

' VBScript.
Dim default
Set default = CreateObject(&quot;WScript.Network&quot;)
default.SetDefaultPrinter &quot;hp deskjet 840c series&quot;
 
Hi Everyone ,
I am posting this question here ,becaus it seems that all the experts are in this thread.

I am trying to create a dummie printer that will send the job to 2 different printer that I already have configured , someone told me that we could do this thru scripting, but I really don't know how to associate the script to print.

It's for our accounting department , they need to print each document on 2 printers , and they keep switching between printers to do their jobs .
Any idea on how to do this ?

Thank you,
David
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top