PDA

View Full Version : Inconsistent Behavior When Capturing Application Events From xla Add-in



ercanyuz
03-19-2008, 01:40 AM
Hi everyone,

I wrote a small .xla add-in for Excel, which puts a command bar on top, and provides several buttons that do some tasks on the workbook that are frequently needed in our office.

At some point, I needed to catch the event of a newly activated sheet, because my add-in will do something upon this event (namely, change the state of one of the buttons on its own command bar)

After some reading and searching, I managed to accomplish the task, however not completely. I first want to copy my code. The code has some tags in it, in order to make it easy to follow the flow and find out where the error lies.

Here is the class module in the xla, which handles the events of the application:


' Class name is EventClassModule
Public WithEvents App As Application

Private Sub Class_Initialize()
MsgBox "Event Class initialized" ' Just to follow
End Sub

Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
MsgBox "WB Opened: " & Wb.Name ' Just to follow
End Sub

Private Sub App_SheetActivate(ByVal Sh As Object)
' Unpress the button if sheet selection changes
App.CommandBars(toolbarName).Controls(3).State = msoButtonUp
MsgBox "A new sheet is activated"
End Sub


Below is the ThisWorkbook module of the xla:


Option Explicit

Dim eventInstance As New EventClassModule

' Events of this workbook
Private Sub Workbook_Open()
MsgBox "xla Workbook opened"
Set eventInstance.App = Application
End Sub

Private Sub Workbook_AddinInstall()
Run ("AddCommandBar")
End Sub

Private Sub Workbook_AddinUninstall()
Run ("RemoveCommandBar")
End Sub


There is of course my standard module which has the functions that act on the workbook upon button clicks, but I don't think that code is relevant in this context.

So, this is it. Quite small and simple, right? But its behaviour is very inconsistent. I install it and the command bar gets set up correctly. When I open a new excel instance from programs menu, the output comes in this order:

1. "xla Workbook opened"
2. "Event Class initialized"
3. "WB Opened: MyAddIn.xla"
4. "WB Opened: Mappe1" (Because my excel is German :) )

The expected behaviour beyond that point would be to see the message "A new sheet is activated" whenever I click on the tabs of the sheets. But this doesn't happen. However, when I open a blank workbook, the message "WB Opened: MappeN" still pops up. That means, I can catch the WorkbookOpen event, but not the SheetActivate event. Very strange.. As far as I found out from debugging so far, I anyhow lose the App object soon after the a new workbook is opened.

I must confess that I occasionally saw this "A new sheet is activated" message, but now I can't regenerate that situation now. It seems to be just random, occasionally it works, but mostly not. (of course there is a reason for it that I cannot see)

For example, it works if I manually re-execute the Workbook_Open event handler in ThisWorkbook module of the xla after I open Excel (Alt+F11 and then F8 inside the function). But I still don't know how to make it work itself. Does that tell anything to you?

So, I would greatly appreciate your comments on this.

Cheers!

Ercan

Bob Phillips
03-19-2008, 02:05 AM
I have just installed that code and everything worked fine for me, I got the message on every sheet.

I didn't create it as an addin, and I commented out the toolbar code, but everything else was good.

Have you put a break in that code to see if anything happens at that point?

Bob Phillips
03-19-2008, 02:06 AM
I was thinking that the line

App.CommandBars(toolbarName).Controls(3).State = msoButtonUp

which I commented out might be the problem.

ercanyuz
03-19-2008, 05:08 AM
Hello,

Thank you for your response. Right after installing the add-in, it works perfectly. But the key to regenerate the error is to restart excel after installing it. After a point, it doesn't work. So I don't think you can regenerate the error case without installing it as an add-in. Like I told before, after stopping functioning, it works correctly again if I manually re-execute the Workbook_Open event handler in ThisWorkbook module of the xla.

Bob Phillips
03-19-2008, 05:13 AM
Aah! I think that is because of the code that you have in AddinInstall. That only happens when you install the addin, not when it is installed and you open the workbook (restart Excel). Move it to Workbook Open and see if that cures the problem.

ercanyuz
03-19-2008, 08:03 AM
Yes, I know. Within AddInInstall event, I set up the command bar. Within Open, I initialize the class module in order to catch the event fired from the other workbooks. If you tell that I should set the command bar each time excel starts, I don't think it's a good idea because the location and settings of the command bar gets lost.

Bob Phillips
03-19-2008, 08:38 AM
But when Excel opens again, the commandbar isn't setup, so that activate code fails, surely?

Andy Pope
03-19-2008, 09:00 AM
FYI: Cross post
http://www.ozgrid.com/forum/showthread.php?t=86776