Events
These are the events than an object can respond to. If we want an object to respond to an event, we enter code into the appropriate event proceduire for that object.
In all the object models, events are only raised by a small number of objects.
Some of the events are duplicated on different objects.
Excel Application Level Events
What is an Event Handler ?
Event handler procedures must be located in the code module for the object that responds to the event.
A procedure that executes when an event occurs is known as an event handler.
Event handler procedures are characterised by the following:
1) They have a special name that is made up of the object followed by an underscore followed by the name of the event.
For example if you write a procedure that executes when an Excel workbook is opened, the procedure must be located in the ThisWorkbook codemodule
2) They are always stored in the code window for the relevant object.
You can introduce events for when your userform controls change. A common event which may be useful is a multipage change event
You can also have an event for the double clicking of a control "_Dblclick".
If you are using the Mouse events for controls be aware that inserting break points amd message boxes will disrupt the order of events and the events may not necessary be fired.
Associating the event-handler with the event you want to respond to is called "trapping the event".
You can disable any events being activated by using the Application.EnableEvents = False.
Recognising AutoMacros
AutoMacros must be places inside code modules.
They can be recognised in two ways:
1) A procedure in any module is given the name of the auto macro.
2) A procedure called Main() is placed inside a module which has the name of the auto macro.
Auto macros can be stored in the following places:
Normal Template
Another Template
Document
In order for an auto macro to run is must be saved in:
Normal Template
Active Document
Template which the active document is based on
The only exception is the AutoExec which must be stored in:
Normal Template
Global Template loaded through the templates and add-ins dialog box
Global Template saved in the startup folder
Documents and Templates
AutoMacros can be stored in a document, a user template, a global template or in your Normal template.
There are lots of combinations of AutoMacros and Document event procedures that can exist.
Lets consider the following scenarios:
Document Scenarios
0) If a document has AutoOpen and AutoClose macros then:
When the document is opened the AutoOpen executes.
When the document is closed the AutoClose executes.
0) If a document has AutoOpen and AutoClose macros and Document_Open and Document_Close events then:
When the document is opened the AutoOpen executes and then the Document_Open event.
When the document is closed the AutoClose executes and then the Document_Close event.
Template Scenarios (startup folder)
0) If a template has AutoExec and AutoExit macros then:
When Word is opened the AutoExec macro executes.
When Word is closed the AutoExit macro executes.
0) If a template has Document_Open and Document_Close events then:
??
0) If a template has AutoOpen and AutoClose macros and Document_Open and Document_Close events then:
Document and Template Scenarios
0) If a document has AutoOpen and AutoClose macros and the template has AutoOpen and AutoClose macros then:
When the document is opened only the AutoOpen from the document executes.
When the document is closed only the AutoClose from the document executes.
There is a rumour that unless the global template is your Normal.dot having this causes a "Compile Error Member already exists in a object module" error in 1997 ??
0) If a document has AutoOpen and AutoClose macros and the template has Document_Open and Document_Close events then:
When the document is opened the document's AutoOpen executes, followed by the template's Document_Open event.
When the document is closed the document's AutoClose executes followed by Document_Close event.
0) If a document has Document_Open and Document_Close events and the template has Document_Open and Document_Close events then:
When the document is opened
When the document is closed
0) If a document has Document_Open and Document_Close events and the template has Document_Open and Document_Close events and AutoOpen and AutoClose macros then:
When the document is opened
When the document is closed
If both a template and a document have Document_Open or Document_Close events then those belonging to the template will be executed first.
Important
Holding down Shift when a document opens will prevent any auto_open macros from running. For example if you create a new document based on a template that contains an AutoNew macro you can hold down Shift when you press Create on the New Document dialog box. If the macro triggers an auto macro you can prevent it from running using the WordBasic.DisableAutoMacros statement. To enable auto macros again afterwards use WordBasic.DisableAutoMacros 0.
© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopNext