Solved

Printing an Office document using VBS on a specified printer

Posted on 2012-03-22
7
2,379 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Author Comment

by:petr_hlucin
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you, this solves the problem
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
No problem. Thanks for the grade.

Regards,

Rob.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now