Solved

Wait for process to complete

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

810 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