Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Printing an Office document using VBS on a specified printer

Posted on 2012-03-22
7
Medium Priority
?
3,206 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
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 2000 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
Cancel future meetings from user mailboxes in Office 365 using Remove-CalendarEvents
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
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 …

722 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