?
Solved

Wait for process to complete

Posted on 2007-12-05
5
Medium Priority
?
2,011 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 2000 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
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 …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

777 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