Solved

Printing an Office document using VBS on a specified printer

Posted on 2012-03-22
7
2,752 Views
Last Modified: 2012-03-28
Let's assume the following VBS script which opens file passed as the first argument in Word (if the file has .docx extension) or Excel (if the file has .xls extension) and prints it on the default printer:
Dim objOffice, oWkbk, strFile


If WScript.Arguments.Count = 0 Then
  ' no arguments passed from command line  
  WScript.Quit
Else
  strFile = Wscript.Arguments(0)

  ' Print XLS document
  If (Right(strFile, 3) = "xls") Then
    Set objOffice = CreateObject("Excel.Application")
    objOffice.Visible = False
    Set oWkbk = objOffice.Workbooks.Open(strFile)
    oWkbk.PrintOut
    oWkbk.Close xlDoNotSaveChanges
    objOffice.Quit
  End If

  ' Print WORD document
  If (Right(strFile, 4) = "docx") Then
    Set objOffice = WScript.CreateObject("Word.Application")
    objOffice.Documents.Open strFile
    objOffice.ActiveDocument.PrintOut
    Wscript.Sleep(1000)
    objOffice.ActiveDocument.Close
    objOffice.Quit
  End If

End If

Open in new window

I wish to modify this script so that I pass an identification of the printer (probably name) on which the document should be printed as second parameter.
0
Comment
Question by:petr_hlucin
  • 4
  • 3
7 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 37755357
Try using

strDefault = objOffice.ActivePrinter
objOffice.ActivePrinter = "\\server\printer"
' print the doc
objOffice.ActivePrinter = strDefault


Regards,

Rob.
0
 
LVL 4

Author Comment

by:petr_hlucin
ID: 37765242
Thank you for your advice but after adding the following line:

objOffice.ActivePrinter = Wscript.Arguments(1)

Open in new window


... running the script caused runtime error:


---------------------------
Windows Script Host
---------------------------
Script:	C:\Users\petr\.student.cz\print.vbs
Line:	17
Char:	7
Error:	Unable to set the ActivePrinter property of the Application class
Code:	800A03EC
Source: 	Microsoft Office Excel

Open in new window


Line 17 and char 7 is the line mentioned above.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37768261
Does your printer name passed at the command line (as the second argument) have spaces in it?

So you're passing the file name as the first argument, and the printer name as the second?  It should be
wscript c:\Scripts\PrintFile.vbs "C:\Files\ExcelDoc.xls" "\\server\Printer1"

and it should work.

To test, above this:
objOffice.ActivePrinter = Wscript.Arguments(1)

put this
MsgBox "Setting activeprinter to " & WScript.Arguments(1)

and see if it's correct.

Regards,

Rob.
0
Technology Partners: 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!

 
LVL 4

Author Comment

by:petr_hlucin
ID: 37770634
Thank you for your answer.

I've double-checked possibility of this problem. The name of the printer is correct and I'm still getting the error mentioned above.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 37774102
OK, according to the user here:
http://www.ozgrid.com/forum/showthread.php?t=15079

In Excel, you need to use the ActivePrinter parameter to the PrintOut method, since the Application.ActivePrinter setting is read-only when using COM automation

BUT

In Word, you *can* use the Application.ActivePrinter method....

So, this should follow those guidelines.

Dim objOffice, oWkbk, strFile
Const xlDoNotSaveChanges = 2

If WScript.Arguments.Count = 0 Then
  ' no arguments passed from command line  
  WScript.Quit
Else
  strFile = Wscript.Arguments(0)

  ' Print XLS document
  If (Right(strFile, 3) = "xls") Then
    Set objOffice = CreateObject("Excel.Application")
    objOffice.Visible = False
    Set oWkbk = objOffice.Workbooks.Open(strFile)
    oWkbk.PrintOut , , , , Wscript.Arguments(1)
    oWkbk.Close xlDoNotSaveChanges
    objOffice.Quit
  End If

  ' Print WORD document
  If (Right(strFile, 4) = "docx") Then
    Set objOffice = CreateObject("Word.Application")
    objOffice.Documents.Open strFile
	strDefault = objOffice.ActivePrinter
	objOffice.ActivePrinter = WScript.Arguments(1)
    objOffice.ActiveDocument.PrintOut
    Wscript.Sleep(1000)
	objOffice.ActivePrinter = strDefault
    objOffice.ActiveDocument.Close False
    objOffice.Quit
  End If

End If

Open in new window


Regards,

Rob.
0
 
LVL 4

Author Closing Comment

by:petr_hlucin
ID: 37776659
Thank you, this solves the problem
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37779449
No problem. Thanks for the grade.

Regards,

Rob.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Outlook Free & Paid Tools
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question