Event for notifying the automation executable on closing Excel

M

Mircea Ion

I need to make my automation executable capable of inhibiting Excel to be
closed from within Excel.
I was thinking that an event might exist that is triggered when Excel is
about to close an I can write a handler that would stop that (by setting a
variable or something).

Thank you
 
C

Cindy M.

Hi Mircea,
I need to make my automation executable capable of inhibiting Excel to be
closed from within Excel.
I was thinking that an event might exist that is triggered when Excel is
about to close an I can write a handler that would stop that (by setting a
variable or something).
Word has a Quit event; Excel does not. Again, I refer you to Andrew
Whitechapel's book, where he states that, as soon as you make the Excel
application visible to the user, the user can exit the application, and this
can't be tracked within the object model. The discussion about how to use the
Windows API to optimally do this covers six pages. In a nutshell, he
recommends to monitor the Excel application window, in a separate thread, to
find out when it's no longer visible. (If you have a reference to the Excel
object in your solution, when the user tries to quit Excel it won't quit,
just not be visible, because you're holding it in memory.) He uses the
Windows API showWindow method for this.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
S

Sly

Cindy,

Yeah, ... you know using API defeats my purpose of writting less code. I've
had enough.
At least not for all the tedious tasks like wrapping API functions,
monitoring from another thread and so on.(I hope you are referring to the
Windows API.)

After a sleepless night I figured out a method so I can accomplish my goal.
I discovered that there is a BeforeClose event for the Workbook which when
triggered calls the handler method by passing to it a reference to the
workbook instance about to be closed and a boolean variableThe boolean is
called Cancel and is passed by reference. If you set the Cancel to true the
workbook will not be closed, ergo Excel can't be closed because every
workbook has to close in order for that to happen.Even when pressing the X
button of the Excel application nothing happens.

So, this is an example where I will allow closing the workbook only when a
certain button is pressed in the automation application (which sets
OkToCloseExcel to true) or if the workbook is not the one I need not to be
closed (e.g. workbooks opened manually by the user) - LtlWorkBook is a
reference to the workbook opened by the automation application.

Private Sub ExcelApp_WorkbookBeforeClose(ByVal Wb As
Microsoft.Office.Interop.Excel.Workbook, ByRef Cancel As Boolean) Handles
ExcelApp.WorkbookBeforeClose
If OkToCloseExcel Or Not Wb.Name = LtlWorkBook.Name Then
Cancel = False 'This will allow closing the workbook
Else
Cancel = True 'This will stop the workbook from being closed
End If
End Sub

So after all the users cannot do whatever they please.

Cheers.


Hi Mircea,> >> I need to make my
automation executable capable of inhibiting Excel to be >> closed from
within Excel.>> I was thinking that an event might exist that is triggered
when Excel is >> about to close an I can write a handler that would stop
that (by setting a >> variable or something).>>> Word has a Quit event;
Excel does not. Again, I refer you to Andrew > Whitechapel's book, where he
states that, as soon as you make the Excel > application visible to the
user, the user can exit the application, and this > can't be tracked within
the object model. The discussion about how to use the > Windows API to
optimally do this covers six pages. In a nutshell, he > recommends to
monitor the Excel application window, in a separate thread, to > find out
when it's no longer visible. (If you have a reference to the Excel > object
in your solution, when the user tries to quit Excel it won't quit, > just
not be visible, because you're holding it in memory.) He uses the > Windows
API showWindow method for this.> > Cindy Meister> INTER-Solutions,
Switzerland> http://homepage.swissonline.ch/cindymeister (last update Jun 17
2005)> http://www.word.mvps.org> > This reply is posted in the Newsgroup;
please post any follow question or > reply in the newsgroup and not by
e-mail :)>
 
C

Cindy M.

Hi Sly,
After a sleepless night I figured out a method so I can accomplish my goal.
I discovered that there is a BeforeClose event for the Workbook
Glad that works for you :)

Cindy Meister
 

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