Excel/PowerPoint Quit event

H

hsuntn

I'm working with Office Application objects in a client/server
WinForm application which allows the user to access remotely
stored files. When the user opens the remote file, it is
downloaded to their machine, and it is opened in the appropriate
Office app.

I've hooked the various BeforeSave events to let me know if/when
they've made modifications to the file, so I can mark it as dirty.
However, I would like to know when the Office application has
terminated, so that I can then upload dirty files back to the
remote server. Word.Application has a Quit event in its parent
interface ApplicationEvents4_Event, so I can hook that. However,
I haven't seen a similar Quit event in the Excel and PowerPoint
objects. Anybody know where it is, or if it exists?

(I'd also like to know when the Office applications quit so that
I can remove the COM references and null out the variables that
point to them.)

Oh, and I can't use the BeforeClose/Close events, because if
the user hits the Close Window button on the frame of the
application, then the document is closed it is saved. (Oddly enough.)

Thanks,
Harold
 
N

Nicholas Paldino [.NET/C# MVP]

Harold,

Since the office applications are processes in themselves, you could get
the Process instance that represents the process that you started, then call
the WaitForExit method, blocking until the application exits. Of course,
you don't want to keep your application hung up while this is happening, so
I would recommend doing this in another thread while waiting (which can then
fire an event upon completion).

Hope this helps.
 
H

hsuntn

Thanks for the reply, Nicholas. I played around with the Process class
as per your suggestion, and indeed it all works beautifully, except...
(knew that was coming, didn't'cha?)

Ok, I seem to have a paradox. In order for the Excel process to
terminate (so my waiting thread unblocks from the WaitForExit()
call), I have to release all references to the Excel.Application
instance, including using the Marshal.ReleaseComObject().
If I do this, Excel dies a peaceful death when the user
closes it, and the WaitForExit() call returns and I can then perform
clean up. Beautiful.

But, if I release all references to the Excel.Application, then the
BeforeSaveEvent that I've hooked to let me know that the workbook
was saved no longer receives events. So now I can't tell if the
file was saved (using that event). I could use file modification time,
I
guess, but I was wondering if there was another way out:
ReleaseComObject(): good for WaitForExit(), bad for BeforeSave
no ReleaseComObject(): bad for WaitForExit(), good for BeforeSave

Many thanks indeed,
Harold
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top