Solved

Wait for process to complete

Posted on 2007-12-05
5
1,942 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
  • 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now