Is it possible to detect when an opened Word document (ie. word is running with a specific doc open) is closed? Note that the Word executable doesn't have to be terminated to close a document. I managed to get notifications when the file changes (shellapi FindFirstChangeNotification) but I haven't found something that can detect whether the file (/ file handle) is open.
I tried it with several api's that require exclusive access to the file, like CreateFile in exclusive mode (ie. all sharing flags off) but Word doesn't care, I can open the file even when it's used. Also tried LockFile but again, it doesn't matter to Word. I suspect it keeps a lot in memory without using the file or a file handle.

Any ideas?

Thomas
Posted on 2002-08-17 03:49:21 by Thomas
I used DDE in a VBA proggie to detect an open document.
Am sure that there is also a way to detect the closing.
On the other hand, you could add "DocumentClose" in
your normal.dot and call a DLL or whatever you want from
there.
Posted on 2002-08-17 04:30:19 by bazik
The program needs to run on every machine with office without the use of macros so I can't :(.. However I did find out that you can't open a file with DELETE access (CreateFile:dwDesiredAccess) when word has it open.
I'm not sure if DELETE exists in win9x/me though.. Can't find it (yet) either.

Thomas
Posted on 2002-08-17 07:24:23 by Thomas
I know you can't close open Word files from explorer, neither on Win98.
Then, I guess this won't be of much help....
Maybe you could try and use GetChildWindow ?
If you find the file name in its caption you could get the window proc and intercept WM_CLOSE !?
Posted on 2002-08-17 07:40:10 by goofee
Maybe you could try and use GetChildWindow ?
If you find the file name in its caption you could get the window proc and intercept WM_CLOSE !?


You can even do file, close and close the document, but keep the window open so that won't work. Besides, you can't be sure you've got the right window because the filename is shown without the path.

Thomas
Posted on 2002-08-17 07:43:25 by Thomas
Instead of VBA you can also write plugins using a dll with the Office SDK.
I think those dlls don't use macros.
Posted on 2002-08-17 07:53:42 by goofee
Thomas,

I'm curious, What is the purpose in capturing the Close Event on a document?

Enjoy your work, P1
Posted on 2002-08-18 10:18:23 by Pone
Thomas,

After reading your post again. I realize some of your problem with MS Office apps is that they most always open a temp file with the original file contents copied into it. That's how they work around the file rights issues when opening a doc. The temp is usually hidden in the same directory with an appended "~$" to the beginning of the original file name. You can watch when Word closes this file to get a simplified monitored document closing event. But I've known Word to leave this file in place, if the user does not have delete rights or crashes. If Word finds this temp file again, it knows to attempt a file recovery.

There are other approaches to your closing event problem, but mainly depend on the purpose you are trying to achieve.

Enjoy your work, P1
Posted on 2002-08-18 10:40:24 by Pone
Thomas,

maybe it is worth to try to hook some functions.
CreateFile->compare the path+name, if OK, get file handle, CloseHandle->compare the handle.
Posted on 2002-08-18 11:48:31 by masquer
I'm curious, What is the purpose in capturing the Close Event on a document?


It's something I need for a tool I'm writing for my work. It's like with winzip, when you extract a file, keep winzip open, modify the file, and go back to winzip; it will ask you if it should update the zip file with the modified file. I need something similar, but for remote editing of word documents (ie. downloading it, editing it, uploading it). Detecting modifcation is no problem but if the user forgets to save the document before going back to the tool nothing will be detected. So it would be nice to warn the user the document is still open and might not have been saved.

You are right about the temp files, you can even modify the document file in another program when it's open. But requesting DELETE access will fail if the document is open so I can use that.

Thanks,

Thomas
Posted on 2002-08-18 15:19:58 by Thomas
You can try requesting DELETE access to see if a Word document is open, but that probably doesn't meet the need. A good idea, imo, will be to use the hook functions and monitor what I believe is WM_MDIDESTROY.
But that surely needs some additional work.
Posted on 2002-08-18 19:20:34 by C.Z.
Thomas,

I was asked to generate a Word app to track shipping customer's own equipment back to the customer.

App was to generated a form from a standard draft with the ability to be added to and updated after the initial creation from the standard draft.

I used the e-mail system (Exchange) to past around links to the documents that points to a custom MASM loader which checks for the proper version of Word (2K), then start it with a custom .dot with programming to make Word into an app.

I did not use the Auto close feature myself in this app . I stubbed all the Auto functions and checked their operations. But If your interested, I can share more information about the app. Just let me know.

If this sound wierd to you, just remember, M$ said this is not a bug, but programmed by design. This was the short discription. There were many trade offs in getting the desired behaivor, that was needed.

The other way is to create a COM add-in ( in MASM of course ) and I am currently working/learning that one myself.

Enjoy your work, P1
Posted on 2002-08-19 09:03:03 by Pone