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?

Recommended Answers

All 2 Replies

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.