Link to home
Start Free TrialLog in
Avatar of teonguyen
teonguyen

asked on

Event in Automation

I'm writing an MFC application that automates Microsoft Excel. In my application, when I click on "RUN" button, MS Excel will be activated and a workbook is opened. So, in my application, how do I capture the event that occurs when I mannually close the workbook and shut down MS Excel ?
ASKER CERTIFIED SOLUTION
Avatar of GloriousRain
GloriousRain

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ambience
Excel application object fires an event WorkbookBeforeClose (quite obvious when by its name) , you need to catch that event Workbook close event .

There WON'T be any Excel shutdown event at all , even if the user closes the Excel mainwindow , you still have a reference to it (remember) so Excel shuts down at your will , when you release the object.

How to catch the event , well for that you need to create object of AppEvents class ... :)

You feel comfortable or want more help??
He just gave you everything .. while i was typing .. yikes ... there is however an easier way than using the switch logic in invoke and doing all that work in CMyEventDispatch . You can add a class that implements IDispatch and then add handler-methods to it , just make sure that whatever name you give to a method , its DISPID should match the DISPID of the event that it is handling ..

for example
if WorkbookBeforeClose has a dispid 0x699 , you can add OnWorkbookBeforeClose with the same signature same dispid, and then add whatever you want to do in that method.