Solved

Printing an Office document using VBS on a specified printer

Posted on 2012-03-22
7
2,849 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

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!

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
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 …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

737 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