Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Event procedures (and the temple of doom)

This is my first try at using event procedures. It's not going well.
I'm trying to run some code whenever a workbook with a certain name is
activated or deactivated. I'd like to keep the workbook free of macros
and keep the code in an add-in instead. If that's not possible,
disregard the rest of this and fetch me an ambulance.

The VBA help says to create a Class Module like this:

Public WithEvents App As Application

and then write event procedures there. And then "connect the declared
object in the class module with the Application object," as follows, by
running this code from any module:

Dim X As New EventClassModule

Sub InitializeApp()
Set X.App = Application
End Sub

This is where I get lost:

1. Where does the 'Dim' statement go? With the global decs?

2. Do I need to run InitializeApp manually at every Excel session?

I've tried various places for InitializeApp; still, neither of the
events I’m trying to recognize will fire. Currently it's in the
ThisWorkbook module of the add-in, near that module’s menu-building
code. If the answer to #2 above is Yes, should InitializeApp be called
from *within* the menu-building code, since that code will run each time
Excel opens?

As I say, neither event fires. Here's the Class Module (action code
simplified):

Option Explicit
Public WithEvents App As Application

Sub App_WorkbookActivate(ByVal Wb As Workbook)
If Wb.Name = "xyz.xls" Then MsgBox "Hi"
End Sub

Sub App_WorkbookDeactivate(ByVal Wb As Workbook)
If Wb.Name = "xyz.xls" Then MsgBox "Bye"
End Sub

What am I leaving out?!?! And assuming the answer is straightforward,
two more specific questions: Does the Workbook_Deactivate event fire
just *before* deactivation or after? (Specifically I'm wondering what
the value of Workbooks.Count is when that procedure begins, i.e., does
it include the workbook being deactivated.) And finally, if only one
workbook is open, does closing it also officialy "deactivate" it, for
purposes of this code?

Please help me understand this. Thanks very very much.

Mark Tangard
"Life is nothing if you're not obsessed." --John Waters

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Event procedures (and the temple of doom)

Perhaps Chip Pearson's explanation will be clearer:

http://www.cpearson.com/excel/AppEvent.htm

--
Regards,
Tom Ogilvy

"Mark Tangard" wrote in message
...
This is my first try at using event procedures. It's not going well.
I'm trying to run some code whenever a workbook with a certain name is
activated or deactivated. I'd like to keep the workbook free of macros
and keep the code in an add-in instead. If that's not possible,
disregard the rest of this and fetch me an ambulance.

The VBA help says to create a Class Module like this:

Public WithEvents App As Application

and then write event procedures there. And then "connect the declared
object in the class module with the Application object," as follows, by
running this code from any module:

Dim X As New EventClassModule

Sub InitializeApp()
Set X.App = Application
End Sub

This is where I get lost:

1. Where does the 'Dim' statement go? With the global decs?

2. Do I need to run InitializeApp manually at every Excel session?

I've tried various places for InitializeApp; still, neither of the
events I’m trying to recognize will fire. Currently it's in the
ThisWorkbook module of the add-in, near that module’s menu-building
code. If the answer to #2 above is Yes, should InitializeApp be called
from *within* the menu-building code, since that code will run each time
Excel opens?

As I say, neither event fires. Here's the Class Module (action code
simplified):

Option Explicit
Public WithEvents App As Application

Sub App_WorkbookActivate(ByVal Wb As Workbook)
If Wb.Name = "xyz.xls" Then MsgBox "Hi"
End Sub

Sub App_WorkbookDeactivate(ByVal Wb As Workbook)
If Wb.Name = "xyz.xls" Then MsgBox "Bye"
End Sub

What am I leaving out?!?! And assuming the answer is straightforward,
two more specific questions: Does the Workbook_Deactivate event fire
just *before* deactivation or after? (Specifically I'm wondering what
the value of Workbooks.Count is when that procedure begins, i.e., does
it include the workbook being deactivated.) And finally, if only one
workbook is open, does closing it also officialy "deactivate" it, for
purposes of this code?

Please help me understand this. Thanks very very much.

Mark Tangard
"Life is nothing if you're not obsessed." --John Waters



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Event procedures (and the temple of doom)

Thanks SO much. This was a lot more, um... penetrable than the help file.

Mark Tangard
"Life is nothing if you're not obsessed." --John Waters

Tom writes:
Perhaps Chip Pearson's explanation will be clearer:

http://www.cpearson.com/excel/AppEvent.htm

--
Regards, Tom Ogilvy

"Mark Tangard" wrote in message
...

This is my first try at using event procedures. It's not going well.
I'm trying to run some code whenever a workbook with a certain name is
activated or deactivated. I'd like to keep the workbook free of macros
and keep the code in an add-in instead. If that's not possible,
disregard the rest of this and fetch me an ambulance.

The VBA help says to create a Class Module like this:

Public WithEvents App As Application

and then write event procedures there. And then "connect the declared
object in the class module with the Application object," as follows, by
running this code from any module:

Dim X As New EventClassModule

Sub InitializeApp()
Set X.App = Application
End Sub

This is where I get lost:

1. Where does the 'Dim' statement go? With the global decs?

2. Do I need to run InitializeApp manually at every Excel session?

I've tried various places for InitializeApp; still, neither of the
events I’m trying to recognize will fire. Currently it's in the
ThisWorkbook module of the add-in, near that module’s menu-building
code. If the answer to #2 above is Yes, should InitializeApp be called
from *within* the menu-building code, since that code will run each time
Excel opens?

As I say, neither event fires. Here's the Class Module (action code
simplified):

Option Explicit
Public WithEvents App As Application

Sub App_WorkbookActivate(ByVal Wb As Workbook)
If Wb.Name = "xyz.xls" Then MsgBox "Hi"
End Sub

Sub App_WorkbookDeactivate(ByVal Wb As Workbook)
If Wb.Name = "xyz.xls" Then MsgBox "Bye"
End Sub

What am I leaving out?!?! And assuming the answer is straightforward,
two more specific questions: Does the Workbook_Deactivate event fire
just *before* deactivation or after? (Specifically I'm wondering what
the value of Workbooks.Count is when that procedure begins, i.e., does
it include the workbook being deactivated.) And finally, if only one
workbook is open, does closing it also officialy "deactivate" it, for
purposes of this code?

Please help me understand this. Thanks very very much.

Mark Tangard
"Life is nothing if you're not obsessed." --John Waters


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop of Doom ARRRG! jlclyde Excel Discussion (Misc queries) 3 December 9th 08 03:02 PM
problems with Add-In event procedures when moving WB files between computers jon Excel Programming 2 April 29th 04 04:47 PM
Event Procedures: Event on Worksheet to fire Event on another Worksheet Kathryn Excel Programming 2 April 7th 04 07:35 PM
Passing ARGUMENTS between event procedures of a USERFORM jason Excel Programming 8 November 10th 03 07:36 PM
Creating Event procedures from a macro Robert Stober Excel Programming 3 September 7th 03 06:52 PM


All times are GMT +1. The time now is 07:49 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"