BLOGS BY TOPIC▼
BLOGS BY AUTHOR▼
BLOGS BY YEAR▼
Want to do something when a user opens a workbook, or stop them clicking on a particular cell? You need to learn how to attach code to Excel workbook or worksheet events!
- Introduction to Handling Events in Excel VBA Code
- Workbook Events
- Events for a Particular Worksheet
- Bypassing Macros
- Considerations for Other MS Office Application Events (this blog)
Posted by Andy Brown on 22 November 2011
You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
Considerations for Other MS Office Application Events
Although this blog is mainly about Excel VBA, I thought I'd briefly show how events work in various other Microsoft Office applications. I've used Office 2010 for all of the examples on this page, but the ideas would work just as well in Office 2007.
MS Word Events
Microsoft Word follows pretty much the same pattern as Microsoft Excel:
You can double-click on ThisDocument to attach code to an event for the document.
The list of events available isn't as rich as it is for Excel:
The list of events doesn't include BeforeClose, for example.
PowerPoint doesn't allow you to attach code to events easily:
There is a presentation open, but you can't see that in Project Explorer.
However, as for Word there is a way to attach code to events for a presentation by creating something called an event sink (see above).
Even if you do create a PowerPoint event sink, you still won't have truly solved the problem, as you still need something to run a macro to create the event sink in the first place!
Microsoft Access Events
The whole of Access form and report design is based around the concept of events:
This diagram shows the events available for a simple label control (a bit of text stuck on a form).
Whereas events in Excel, Word and PowerPoint are optional extras, you won't be able to write VBA code in Access forms or reports without using them all the time.
Microsoft Outlook Events
The events which can occur in an Outlook session are shown below:
Want to create a rule for how new mail should be treated? Don't bother with the Outlook rules - why not create your own in VBA!
Outlook macros are all held in a single file with an .OTM extension.
As the above examples show, handling events in Excel VBA is - as is so often the case - easier than doing the same thing in other MS Office applications.