Solved

Printing an Office document using VBS on a specified printer

Posted on 2012-03-22
7
2,991 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
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 …

632 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