| | |
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.
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.
•
•
Join Date: Jun 2005
Posts: 70
Reputation:
Solved Threads: 2
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
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
![]() |
Similar Threads
- Best Way to Check for same Word in string? (Java)
- Send data on a serial port (C++)
- Urgent help needed......Plz somebody help me..... (Windows NT / 2000 / XP)
- Aurora/DrPmon/shopathomeselect..Please Help Me.. (Viruses, Spyware and other Nasties)
- Vb Runtime Files (Visual Basic 4 / 5 / 6)
- Word hangs when saving (Windows NT / 2000 / XP)
- word count (Java)
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: Database question
- Next Thread: Target Line
| Thread Tools | Search this Thread |
* 6 429 2007 access activex add age application basic beginner birth bmp calculator cd cells.find click client code college component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report save search sendbyte sites sort sql sql2008 sqlserver subroutine tags textbox time urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows






