944,108 Members | Top Members by Rank

Ad:
Nov 7th, 2005
0

How to detect when Word has closed

Expand Post »
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?
Similar Threads
Reputation Points: 11
Solved Threads: 2
Junior Poster in Training
w00dy is offline Offline
70 posts
since Jun 2005
Nov 8th, 2005
0

Re: How to detect when Word has closed

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.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Nov 8th, 2005
0

Re: How to detect when Word has closed

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
Reputation Points: 11
Solved Threads: 2
Junior Poster in Training
w00dy is offline Offline
70 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Database question
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Target Line





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC