E
Ed
I have a set of macros that I would like to run whenever any workbook
opens (XL2003). I looked in Help under the WorkbookOpen event, and it
seemed like what I wanted. Then I started getting bogged down in
creating a new Class module declaring an application object with
events??!?
I can copy code into a module with the best of them! But I'm having a
hard time understanding what I'm doing. I'm not familiar with Class
modules, and am not sure how all this works together.
Before you can use events with the Application object, you must create
a new class module and declare an object of type Application with
events. For example, assume that a new class module is created and
called EventClassModule. The new class module contains the following
code.
Public WithEvents App As Application
After the new object has been declared with events, it appears in the
Object drop-down list box in the class module, and you can write event
procedures for the new object. (When you select the new object in the
Object box, the valid events for that object are listed in the
Procedure drop-down list box.)
Before the procedures will run, however, you must connect the declared
object in the class module with the Application object. You can do
this with the following code from any module.
Dim X As New EventClassModule
Sub InitializeApp()
Set X.App = Application
End Sub
After you run the InitializeApp procedure, the App object in the class
module points to the Microsoft Excel Application object, and the event
procedures in the class module will run when the events occur.
**************************
Okay - so I create a new Class module and "Public WithEvents App As
Application" is ~all~ I put in it? Then in any macro I want to use
this I put
Dim X As New EventClassModule
Sub InitializeApp()
Set X.App = Application
End Sub
at the top of the module? Then I can write events, such as
WorkbookOpen, for the object "X"?
Occurs when a workbook is opened.
Private Sub object_WorkbookOpen(ByVal Wb As Workbook)
object An object of type Application declared with events in a
class module. For more information, see Using Events with the
Application Object.
Wb The workbook.
Example
This example arranges all open windows when a workbook is opened.
Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
Application.Windows.Arrange xlArrangeStyleTiled
End Sub
**************************
So if my new object is called "X", then this would actually look like
Private Sub X.WorkbookOpen(ByVal Wb As Workbook)
????
And somewhere I would have to set Wb = the workbook being opened to
pass into this?
If anyone can help my muddled brain make sense of this, I would
appreciate it.
Ed
opens (XL2003). I looked in Help under the WorkbookOpen event, and it
seemed like what I wanted. Then I started getting bogged down in
creating a new Class module declaring an application object with
events??!?
I can copy code into a module with the best of them! But I'm having a
hard time understanding what I'm doing. I'm not familiar with Class
modules, and am not sure how all this works together.
**************************From the Help file "Using Events with the Application Object":
Before you can use events with the Application object, you must create
a new class module and declare an object of type Application with
events. For example, assume that a new class module is created and
called EventClassModule. The new class module contains the following
code.
Public WithEvents App As Application
After the new object has been declared with events, it appears in the
Object drop-down list box in the class module, and you can write event
procedures for the new object. (When you select the new object in the
Object box, the valid events for that object are listed in the
Procedure drop-down list box.)
Before the procedures will run, however, you must connect the declared
object in the class module with the Application object. You can do
this with the following code from any module.
Dim X As New EventClassModule
Sub InitializeApp()
Set X.App = Application
End Sub
After you run the InitializeApp procedure, the App object in the class
module points to the Microsoft Excel Application object, and the event
procedures in the class module will run when the events occur.
**************************
Okay - so I create a new Class module and "Public WithEvents App As
Application" is ~all~ I put in it? Then in any macro I want to use
this I put
Dim X As New EventClassModule
Sub InitializeApp()
Set X.App = Application
End Sub
at the top of the module? Then I can write events, such as
WorkbookOpen, for the object "X"?
**************************From the Help file "WorkbookOpen Event":
Occurs when a workbook is opened.
Private Sub object_WorkbookOpen(ByVal Wb As Workbook)
object An object of type Application declared with events in a
class module. For more information, see Using Events with the
Application Object.
Wb The workbook.
Example
This example arranges all open windows when a workbook is opened.
Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
Application.Windows.Arrange xlArrangeStyleTiled
End Sub
**************************
So if my new object is called "X", then this would actually look like
Private Sub X.WorkbookOpen(ByVal Wb As Workbook)
????
And somewhere I would have to set Wb = the workbook being opened to
pass into this?
If anyone can help my muddled brain make sense of this, I would
appreciate it.
Ed