• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2759
  • Last Modified:

Create Network Printer with VBscript - Direct Printing

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 shell.run 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 "\\192.168.1.1\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 = "192.168.1.5"
PrinterName = "Color Printer - Sharp"
infPath = "\\192.168.1.1\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
objNewPort.Put_

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"
WScript.Quit
0
ddotson
Asked:
ddotson
  • 6
  • 4
1 Solution
 
RobSampsonCommented:
Hi, try it without the first and last quote (the Chr(34)s):
objShell.Run "%comspec% /c 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"

Or even without the %comspec% /c" seeing as rundll32 should be able to be directly inititiated:
objShell.Run "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"

Regards,

Rob.
0
 
RobSampsonCommented:
Also, if you that to a variable, you can then use that as an InputBox, to output it, which you can copy and paste straight into Start --> Run for testing.  And if it doesn't work, you can change it in the Start --> Run box and then paste it back into the InputBox:

strCommand = "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"
strCommand = InputBox("Prompt", "Title", strCommand)
objShell.Run strCommand, 1, True

Regards,

Rob.
0
 
ddotsonAuthor Commented:
This didn't work.  I'm getting "Operation could not be completed".  I think that is an error message generated by Printui.dll.  When output to a msgbox, it all looks good: rundll32 printui.dll,PrintUIEntry /ga /if /b "Color Printer - Sharp" /f "\\192.168.1.1\sn0hwenu.inf" /r "IP_192.168.1.5" /m "SHARP MX-4501N PS" /z
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ddotsonAuthor Commented:
Did some testing and found that a strange character "@" in my original code is killing printui.dll.  It was in the path to the INF file.  Removed it, corrected the path to reflect the change, and it works.  Also, removing the %comspec% worked.  In fact, here's the last line of my code:

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
objNewPort.Put_

addPrinter = "rundll32 printui.dll,PrintUIEntry /ga /if /b """ & printerName & """ /f """ & infPath & """ /r """ & portName & """ /m """ & DriverName & """ /z"
Set objShell = CreateObject("WScript.Shell")
objShell.Run addPrinter
WScript.Quit
0
 
RobSampsonCommented:
Great!  Congrats on getting that to work.  I never saw an "@" in your original code, but as long as you figured it out.

So are there any more problems, or can you close the question by accepting an answer?

Regards,

Rob.
0
 
RobSampsonCommented:
Oh, and a bit more info for you on a possible trap of using cmd /c.....
Try to avoid it if possible, and just directly call the executable that you need.
When you use cmd /c you can run into problems with the use of double-quotes, because if you have more than one set of quotes, the command line can misinterpret them, and throw up errors, or not work as expected.

This is from the help text of cmd:
'=============
If /C or /K is specified, then the remainder of the command line after
the switch is processed as a command line, where the following logic is
used to process quote (") characters:

    1.  If all of the following conditions are met, then quote characters
        on the command line are preserved:

        - no /S switch
        - exactly two quote characters
        - no special characters between the two quote characters,
          where special is one of: &<>()@^|
        - there are one or more whitespace characters between the
          the two quote characters
        - the string between the two quote characters is the name
          of an executable file.

    2.  Otherwise, old behavior is to see if the first character is
        a quote character and if so, strip the leading character and
        remove the last quote character on the command line, preserving
        any text after the last quote character.

'================

Regards,

Rob.
0
 
ddotsonAuthor Commented:
That stuff helps.

The @ symbol was in the actual code - not the "public" version I posted here.  Doing that can be risky - as ppl can't diagnose all aspects of the code.
0
 
RobSampsonCommented:
Yeah, that's true.  So can this question be closed then?  If you still need work on it, it's probably best to accept an answer here and open a new question.

Regards,

Rob.
0
 
ddotsonAuthor Commented:
Thanks for the help.  We are done!
0
 
RobSampsonCommented:
Nice.  Thanks for the grade. ;-)
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now