Solved

Wait for process to complete

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 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