Overview
This project demonstrates how to use the CXllApp class to add event
handler functions for COM Application events such as WorkbookOpen
and SheetChange.
Features
The sample displays the following features:
- Registers handlers for each of the Excel Application
object's main events.
- Adds a new menu to Excel's application menu bar,
allowing the user to switch COM event handling on and off.
- The event handler for SheetBeforeRightClick disables
right-clicking in cell A1 of any sheet, whenever
event handling is switched on.
Implementation
The following steps were significant in creating and completing the
application.
- When the project was generated by the XLL+ AppWizard, the
Override OnXllOpenEx() & OnXllClose()? check-box
was ticked, so that instances of these two virtual methods
were declared and generated automatically.
- Code was added to CComEventsApp::OnXllOpenEx to
(i) create a menu (ii) register the COM event handlers
and (iii) switch on event handling.
- Code was added to OnXllClose to destroy the menu.
- 14 event handler add-in functions were written,
one for each of the events we wanted to trap. (Generally,
you will not want to trap all these events; 2 or 3 usually suffices.)
Each event handler's signature matched the specification in the
documentation for
CXllApp::SetComEventHandler.
- Each of the event handlers is registered using CXllApp::SetComEventHandler.
- Event handling is switched on and off using
CXllApp::SetSinkEvents.
Functions used
Project files
If you are using MS Developer Studio 6, then you should open the project file
ComEvents.dsp.
If you are using MS Visual Studio .NET 2002, then you should open the solution file
ComEvents.sln
or the project file ComEvents.vcproj.
If you are using MS Visual Studio .NET 2003, then you should open the solution file
ComEvents71.sln
or the project file ComEvents71.vcproj.
If you are using MS Visual Studio 2005, then you should open the solution file
ComEvents8.sln
or the project file ComEvents8.vcproj.
See Also
List of Sample Projects
| Samples and Walkthroughs