VBScript to print on secondary printer

I have a VBScript that creates a .txt file, saves and closes it. I need for that script to print on a secondary printer, not the default, without changing the default printer since it may be in use at the same time.

Right now, this prints properly on the default printer:

oWS.Run "NotePad.exe /p " + path

where path is defined as the path to the txt file.

Someone suggested I try this:

oWS.Run "NotePad.exe /p " + path + " " + officePrinter

where officePrinter is defined as '\\OFFICE\HPOffice which is a network printer but the final version will be an attached one. However, this doesn’t work and returns an error implying it is trying to print two files, path and officePrinter with a .txt extension but nothing prints.

Any ideas?

print /d:\\OFFICE\HPOffice filetoprint.txt

Syntax error
Char: 7 (on that line)
Code: 800a03ea

gotta love VBScript (not!). The command line I gave you was supposed to be a regular command prompt command - not a VBScript instruction. Sorry for the confusion. You need to find a way to invoke the command interpreter from VBScript - I can’t remember how you do it since last time I used VBScript was last century :wink:

Maybe this could be your chance to pick up some PowerShell instead of that ugly VBScript? Mucho nicer.

I don’t even know VBScript. Someone else wrote it for me. The whole script works if I use the default printer but I need to use the secondary.

ok, try using

Dim oShell
Set oShell = CreateObject("WScript.Shell")
oShell.Exec "print /d:\\\\server\\printer file.txt"

Didn’t you ask this in another thread? :confused:

I could have sworn I had answered this already. According to a google search, this should get you what you need:

notepad.exe /pt %1 %2 where %1 represents the file name and %2 the printer name.


I did answer this once before: http://www.sitepoint.com/forums/showthread.php?t=587875&highlight=notepad.exe (at least I’m not going totally crazy…)

Did that not work?

That worked for the default printer, I think, but not for the secondary. In my list of printers, the secondary is named HPOffice, as above, but putting that name in gives an error as if it thinks HPOffice is a txt file. Now, HPOffice is also a networked printer (but not at the location this will go in) so I also tried \\OFFICE\HPOffice, which is the name of the networked printer.

EDIT: The error is “The system can’t find the path specified”

This simple script does not work either and gives the error “The filename, directory name or volume is incorrect.”

'define vars
dim officePrinter
officePrinter = "\\\\OFFICE\\HPOffice"
path = "C:\\Users\\Store\\Desktop\	emp.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set oWS = WScript.CreateObject("WScript.Shell")

    'enable this to print
    oWS.Run "NotePad.exe /pt " + path + " " + officePrinter

'destroy objects.  I'm not sure this is necessary
Set oWS = nothing
Set fso = nothing

And a big thanks to my FreeBSD friends for telling me that it’s a capital ‘P’ to make this work, not lower case. :slight_smile: I didn’t need the ‘t’ either.

EDIT: Well, it worked for my simple example above. When I enter that into the full code listing above, I get the directory errors like before.

EDIT: And now it changed again. Getting the error where it thinks the path name to the printer is a text fiile.

Could you try something like this instead? You would need to read the file to be printed into theMessage and then print that.


Dim objFS 'VBScript File System Object
Dim objWSHNet 'Windows Script Host Network Object
Dim objPrinter 'Printer Object to stream text to
Dim theMessage

theMessage = "this is a test to auto print from web site to network printer!!!"

Set objFS = CreateObject("Scripting.FileSystemObject")
Set objWSHNet = CreateObject("WScript.Network")

objWSHNet.AddWindowsPrinterConnection "Canon iR2270/iR2870 PS3", "Canon iR2270/iR2870 PS3", "localhost@lp"
objFS.CreateTextFile newMsg, true, true

Set objPrinter = objFS.CreateTextFile ("canonPrinter:", false)


objPrinter.Close   : set objPrinter = nothing

No because it’s for a Canon printer. :slight_smile:

I’ve seen similar code in my search online but haven’t had a chance to see how to make it work. I’ll try that in a couple hours. Thanks.

That’s just the sample I copy/pasted. All you should need is this format (if I understand what I’m reading):

objWSHNet.AddWindowsPrinterConnection “\\OFFICE\HPOffice”

Here’s another source for this: http://www.computerperformance.co.uk/Logon/LogonScript_Printer_Method.htm

Line 12, invalid procedure call
I changed ‘newMsg’ to ‘theMessage’ since it’s not defined earlier and I got the test on a file on the desktop but it returns the same error for line 15.

I may have to just rewrite the code to use the receipt printer for now or put one of my Unix boxes down there. I know my code works but I’m not sure if the receipt printer prints fast enough. The script on my Unix box works, too, but having a second box means a second printer which is equally inconvenient.

Found an answer to my question. Windows does not allow scripts to print to anything but the default printer! Can you believe that? What a POS.