PC Review


Reply
Thread Tools Rate Thread

Changing default menu item behavior

 
 
=?Utf-8?B?TWVodWw=?=
Guest
Posts: n/a
 
      20th Sep 2007
I am doing Excel Automation and I notice that when I close the Excel window
launched from my main form, EXCEL.EXE still remains in my Task Manager. I
have a cleanup routine in my main form which kills the Excel process when my
main form is closed. But, I would like the ability to detect when the user is
closing the Excel window without closing the main form and still be able to
run the cleanup function. Is there a way to detect such window closing event?

If not, I can disable the close button for the Excel window, but users would
still be able to use the File>Exit menu item to close. In that case, I would
like to change the behavior of the Exit menu item so that I can run my clean
up function when File>Exit is clicked. Is it possible to do so?

I am using C#, but VBA sample code would be helpful too.

Thank you so much.
 
Reply With Quote
 
 
 
 
=?Utf-8?B?VG9tIE9naWx2eQ==?=
Guest
Posts: n/a
 
      20th Sep 2007
Sounds like you have problems in your code. If you fully qualify your
references to excel and clear those references, then excel should close
properly when a quit command issued.

http://support.microsoft.com/default...b;en-us;317109
Office application does not quit after automation from Visual Studio .NET
client

--
Regards,
Tom Ogilvy


"Mehul" wrote:

> I am doing Excel Automation and I notice that when I close the Excel window
> launched from my main form, EXCEL.EXE still remains in my Task Manager. I
> have a cleanup routine in my main form which kills the Excel process when my
> main form is closed. But, I would like the ability to detect when the user is
> closing the Excel window without closing the main form and still be able to
> run the cleanup function. Is there a way to detect such window closing event?
>
> If not, I can disable the close button for the Excel window, but users would
> still be able to use the File>Exit menu item to close. In that case, I would
> like to change the behavior of the Exit menu item so that I can run my clean
> up function when File>Exit is clicked. Is it possible to do so?
>
> I am using C#, but VBA sample code would be helpful too.
>
> Thank you so much.

 
Reply With Quote
 
=?Utf-8?B?TWVodWw=?=
Guest
Posts: n/a
 
      20th Sep 2007
Tom,

Thanks for the prompt reply, but I think you misunderstood my question.

My cleanup function correctly clears all my excel references and calls the
Quit() function...so whenever the cleanup function gets called, everything is
fine. My problem is that when a user manually clicks the close button on an
excel window, I am not being able to detect that close event, and thus my
cleanup function does not even get called.

Does that make sense? Below is my cleanup function which works correctly.

private void cleanUpExcel()
{
if (excelApplication != null)
{
try
{
excelApplication.DisplayAlerts = false;
foreach (Excel.Workbook workbook in excelApplication.Workbooks)
{
foreach (Excel.Worksheet worksheet in workbook.Worksheets)
{

System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
}
workbook.Close(false, Type.Missing, Type.Missing);

System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
}

System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApplication.Workbooks);
excelApplication.Quit();

System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApplication);
}
catch (Exception)
{
//Excel may have been closed via Windows Task Manager. Skip
the close.
}
}
}

Mehul.

"Tom Ogilvy" wrote:

> Sounds like you have problems in your code. If you fully qualify your
> references to excel and clear those references, then excel should close
> properly when a quit command issued.
>
> http://support.microsoft.com/default...b;en-us;317109
> Office application does not quit after automation from Visual Studio .NET
> client
>
> --
> Regards,
> Tom Ogilvy
>
>
> "Mehul" wrote:
>
> > I am doing Excel Automation and I notice that when I close the Excel window
> > launched from my main form, EXCEL.EXE still remains in my Task Manager. I
> > have a cleanup routine in my main form which kills the Excel process when my
> > main form is closed. But, I would like the ability to detect when the user is
> > closing the Excel window without closing the main form and still be able to
> > run the cleanup function. Is there a way to detect such window closing event?
> >
> > If not, I can disable the close button for the Excel window, but users would
> > still be able to use the File>Exit menu item to close. In that case, I would
> > like to change the behavior of the Exit menu item so that I can run my clean
> > up function when File>Exit is clicked. Is it possible to do so?
> >
> > I am using C#, but VBA sample code would be helpful too.
> >
> > Thank you so much.

 
Reply With Quote
 
=?Utf-8?B?VG9tIE9naWx2eQ==?=
Guest
Posts: n/a
 
      20th Sep 2007
This group is primarily for internal VBA issues. Try asking over in the
below group:

http://www.microsoft.com/office/comm...&lang=en&cr=US

--
Regards,
Tom Ogilvy


"Mehul" wrote:

> Tom,
>
> Thanks for the prompt reply, but I think you misunderstood my question.
>
> My cleanup function correctly clears all my excel references and calls the
> Quit() function...so whenever the cleanup function gets called, everything is
> fine. My problem is that when a user manually clicks the close button on an
> excel window, I am not being able to detect that close event, and thus my
> cleanup function does not even get called.
>
> Does that make sense? Below is my cleanup function which works correctly.
>
> private void cleanUpExcel()
> {
> if (excelApplication != null)
> {
> try
> {
> excelApplication.DisplayAlerts = false;
> foreach (Excel.Workbook workbook in excelApplication.Workbooks)
> {
> foreach (Excel.Worksheet worksheet in workbook.Worksheets)
> {
>
> System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
> }
> workbook.Close(false, Type.Missing, Type.Missing);
>
> System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
> }
>
> System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApplication.Workbooks);
> excelApplication.Quit();
>
> System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApplication);
> }
> catch (Exception)
> {
> //Excel may have been closed via Windows Task Manager. Skip
> the close.
> }
> }
> }
>
> Mehul.
>
> "Tom Ogilvy" wrote:
>
> > Sounds like you have problems in your code. If you fully qualify your
> > references to excel and clear those references, then excel should close
> > properly when a quit command issued.
> >
> > http://support.microsoft.com/default...b;en-us;317109
> > Office application does not quit after automation from Visual Studio .NET
> > client
> >
> > --
> > Regards,
> > Tom Ogilvy
> >
> >
> > "Mehul" wrote:
> >
> > > I am doing Excel Automation and I notice that when I close the Excel window
> > > launched from my main form, EXCEL.EXE still remains in my Task Manager. I
> > > have a cleanup routine in my main form which kills the Excel process when my
> > > main form is closed. But, I would like the ability to detect when the user is
> > > closing the Excel window without closing the main form and still be able to
> > > run the cleanup function. Is there a way to detect such window closing event?
> > >
> > > If not, I can disable the close button for the Excel window, but users would
> > > still be able to use the File>Exit menu item to close. In that case, I would
> > > like to change the behavior of the Exit menu item so that I can run my clean
> > > up function when File>Exit is clicked. Is it possible to do so?
> > >
> > > I am using C#, but VBA sample code would be helpful too.
> > >
> > > Thank you so much.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing Default Paste behavior D Riggins Microsoft Excel Misc 1 5th Nov 2008 07:56 PM
Changing a default behavior of RichTextBox MindWanderer Microsoft Dot NET Framework Forms 3 3rd Apr 2008 02:39 PM
changing default behavior for saved web pages =?Utf-8?B?Z2prNzM1?= Windows XP General 3 17th Jul 2006 03:55 PM
Changing IE's default editor for 'View Source' menu item P James Windows XP Internet Explorer 5 16th Nov 2004 01:51 AM
Changing Default Behavior of 'Add Existing Item...'? Kenneth Love Microsoft Dot NET 0 28th Oct 2004 04:43 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:45 AM.