Solved

Wait for process to complete

Posted on 2007-12-05
5
1,979 Views
Last Modified: 2010-09-30
Hi, The following VBS prints a Word doc that has been passed from another computer. It works okay, but I have just guessed at how long to wait for the print job to finish before deleting the local file. I considered monitoring the Winword process and deleting once it is not running, but it is possible that the user may have another Word doc open when the script runs and the Winword process will continue to run.

Is there a more elegant way to have the script delete the file once it has been printed? It is possible the user may have another Word doc open locally when this script runs.

Thanks, Bill


'Receive command line arguments
set args = WScript.Arguments
num = args.Count
 
if num = 0 then
   WScript.Echo "There is no document to print!"
   WScript.Quit 1
end if
 
'Print each document using MS Word
for k = 0 to num - 1
   Set objWord = CreateObject("Word.Application")
   Set objDoc = objWord.Documents.Open("C:\" & args.Item(k))
   objDoc.PrintOut
   objWord.Quit	
next
 
Wscript.Sleep 25000
 
'Delete temporary files from local drive
for k = 0 to num - 1
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set aFile = fso.GetFile("C:\" & args.Item(k))
   aFile.Delete
next

Open in new window

0
Comment
Question by:westone
[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
  • 2
  • 2
5 Comments
 
LVL 12

Expert Comment

by:chandru_sol
ID: 20412926
Does the wscript.sleep doesn't work?
0
 

Author Comment

by:westone
ID: 20413827
As stated in the original post, it works, but I am just guessing at the time needed to print before deleting the file. It is not possible for me to predict all scenarios which may cause a delay in printing the document. And I don't want to have the script waiting in the background when it won't be necessary most of the time.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 20538905
Instead of this:
'Print each document using MS Word
for k = 0 to num - 1
   Set objWord = CreateObject("Word.Application")
   Set objDoc = objWord.Documents.Open("C:\" & args.Item(k))
   objDoc.PrintOut
   objWord.Quit      
next
 
Wscript.Sleep 25000


Try this:

'Print each document using MS Word
Set objWord = CreateObject("Word.Application")
For k = 0 To num - 1
   Set objDoc = objWord.Documents.Open("C:\" & args.Item(k))
   objDoc.PrintOut
Next
 
While objWord.BackgroundPrintingStatus > 0
   WScript.Sleep 100
Wend

objWord.Quit

'See http://msdn2.microsoft.com/en-us/library/aa223963(office.11).aspx

Regards,

Rob.
0
 

Author Comment

by:westone
ID: 20539033
Hey that's a great solution. I've learned something useful, BackgroundPrintingStatus. Thanks.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20539048
No problem.  I jogged my memory to think of that....remembered I'd used a printing status thing before, searched MS site, and there it was....glad it worked for you.

Regards,

Rob.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

730 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