How to detect when Word has closed

Reply

Join Date: Jun 2005
Posts: 70
Reputation: w00dy is an unknown quantity at this point 
Solved Threads: 2
w00dy w00dy is offline Offline
Junior Poster in Training

How to detect when Word has closed

 
0
  #1
Nov 7th, 2005
During the program, Word is opened and displays a document.
When Word is closed by clicking X or selecting File>Exit, the program must detect the event and reset controls.
How is that done, anyone?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: How to detect when Word has closed

 
0
  #2
Nov 8th, 2005
Ultimately, the concept differs depending on your application. If Word was LAUNCHEDwith YOUR Application, Then you can use the API Call "Waitforsingleobject" or (if your program doesn't need to do anything while word is launched) you can use WSH with shell and the .run method. With the latter, you can make it wait until the app launched is closed before proceeding to the next line of code!

The alternatives to either, is a bit more messy (these are the only options I know of if word was NOT launched by YOUR Application), is to use the "findwindow" API Call in a timer control and search for the class name of word. You could also use the findwindow API Call, and instead of the class name for word, use the title in the titlebar of the window. This is not a good way, because if an instance of word is already running, you'll have to figure out which one to close. If you know what is in the titlebar, it's easy enough, but knowing the title bar caption isn't very easy, because it can change depending on the file name and other variable factors. Certainly you can loop through all of the running instances, but figuring out which one to close (assuming more than 1 copy of word is running) is uh, well, you get the point.

The other option, not much easier, is to enumerate the process list, search for the word executable file, grap it's PID (process ID), and check if it's still running (in a timer). Here also, you'll have to hope a copy isn't already running, and if so, grab the PID of any running word processes, and save them in an array. Then, make sure not to check on those processes, but the one in question. I hope this isn't too confusing (the way I've been trying to explain it), but the best solution is to A) make sure YOUR app launches word OR make sure no other instance of word is running and use findwindow. Let me know if this helps any, or if I can help you any further with this.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 70
Reputation: w00dy is an unknown quantity at this point 
Solved Threads: 2
w00dy w00dy is offline Offline
Junior Poster in Training

Re: How to detect when Word has closed

 
0
  #3
Nov 8th, 2005
Thanks for that, Comatose,
the project I'm working on at the moment could have multiple instances of Word running at once (all launched from my application), but I decided to limit it to one at a time for the moment, so I will follow your advice re. that situation for now.
However, I see the problems that can arise . . . my next project will be dealing with multiple documents.
Will let you know how it goes
w00dy
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC