Excel/PowerPoint Quit event  
Author Message
beihong





PostPosted: Thu Jul 14 12:47:35 CDT 2005 Top

Visual C#.Net >> Excel/PowerPoint Quit event 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

DotNet172  
 
 
Nicholas





PostPosted: Thu Jul 14 12:47:35 CDT 2005 Top

Visual C#.Net >> Excel/PowerPoint Quit event 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.


--
- Nicholas Paldino [.NET/C# MVP]
- EMail@HideDomain.com

<EMail@HideDomain.com> wrote in message
news:EMail@HideDomain.com...
> 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
>


 
 
hsuntn





PostPosted: Fri Jul 15 10:33:11 CDT 2005 Top

Visual C#.Net >> Excel/PowerPoint Quit event 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