troubleshooting Question

Create Network Printer with VBscript - Direct Printing

Avatar of ddotson
ddotson asked on
Visual Basic ClassicVB Script
10 Comments1 Solution2811 ViewsLast Modified:
I would like to create a network printer (where the workstation is printing directly via a port) with a VBscript.  I've got the script setup so that it's creating the port properly, but the actual printer creation isn't working.  I can get it to work if I use the command line tool "printui.dll", but that involves calling a batch file, which I don't want to do.  I want to run this all with one script.  So, I've inserted the working text of the batch file into a command.  Somehow, I'm not doing something right.  It's all in the parenthesis or chr(34).

Here's the working code from the batch file:
rundll32 printui.dll,PrintUIEntry /ga /if /b "Printer Name" /f "\\\sn0hwenu.inf" /r "IP_192.168.1.5(Sharp MX-4501N)" /m "SHARP MX-4501N PS" /Z

Here's the code from my script:

Dim portName, IPaddress, printerName, infPath, DriverName, strComputer, objShell

portName = "IP_192.168.1.5"
IPaddress = ""
PrinterName = "Color Printer - Sharp"
infPath = "\\\sn0hwenu.inf"
DriverName = "SHARP MX-4501N PS"

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objNewPort = objWMIService.Get ("Win32_TCPIPPrinterPort").SpawnInstance_

objNewPort.Name = portName
objNewPort.Protocol = 1
objNewPort.HostAddress = IPaddress
objNewPort.PortNumber = "9100"
objNewPort.SNMPEnabled = True

Set objShell = CreateObject("WScript.Shell")
objShell.Run "%comspec% /c " & chr(34) & "rundll32 printui.dll,PrintUIEntry /ga /if /b """& printerName & """ /f """ & infPath & """ /r """ & portName & """ /m """ & DriverName & """ /Z" & chr(34)
msgbox "%comspec% /c " & chr(34) & "rundll32 printui.dll,PrintUIEntry /ga /if /b """& printerName & """ /f """ & infPath & """ /r """ & portName & """ /m """ & DriverName & """ /Z" & chr(34)
'rundll32 printui.dll,PrintUIEntry /ga /if /b "& printerName & " /f " & infPath & " /r " & portName & " /m " & DriverName & " /Z"
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 10 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros