PDA

View Full Version : how to make add-in load before workbook



xltrader100
08-01-2012, 11:03 PM
I have an add-in that runs whenever Excel runs. It has to be running at the time it's companion workbook opens, but if Excel is started up by double clicking the workbook, then the workbook loads before the add-in and things get ugly.

How can I force the add-in to load before the workbook in that situation?

Bob Phillips
08-02-2012, 12:22 AM
What happens when 'things get ugly'.

xltrader100
08-02-2012, 12:41 AM
When the add-in detects that its soul mate is opening, it initializes a lot of variables that are contained in the workbook, and the workbook expects them to be initialized when it opens.

Bob Phillips
08-02-2012, 01:32 AM
Sorry to be dense, but that sentence reads like this to me ... 'when the workbook opens, something should happen, and the addin detects that it is opening, so it makes those things happen'. In other words, everything is hunky-dory. Obviously, everything is not otherwise you wouldn't be here, but I am not getting any idea of what is going wrong.

xltrader100
08-02-2012, 06:42 AM
I didn't word that very well. I should have said that this is what SHOULD happen, but doesn't. It works correctly if the a Add-in is running at the time the workbook opens, but when Excel is not running, and the workbook is double clicked to start it up, then the workbook opens before the Add-in has had time to get everything initialized. And my question was, how to make the Add-In load first.

Paul_Hossler
08-04-2012, 08:52 AM
1. Could you just leave the add in loaded? Probably the easiest

2. The other WB_Open could check to see if the add in is installed, install it, and then continue (or throw an error message)

Paul

xltrader100
08-06-2012, 11:30 AM
After it opens, the Addin stays loaded. It runs whenever Excel runs. The problem is that it doesn't have a chance to start up in time to init globals for the workbook if Excel is started by double clicking the workbook.

Your second suggestion is very close to what I've been doing, but I'm looking for a better way.

Currently, the Workbook_Open procedure defines a public variable, and then no more workbook code runs until the initialization is done by the Addin. The Addin eventually starts up and goes into a loop checking for the existence of the public var set by the workbook. When it finds the var, it knows the workbook is open so it does the initialization and then nulls the variable so it doesn't get found again until the next Workbook_Open event.

I've tried using application events, specifically the app_WorkbookOpen event to notify the Addin that it's workbook has arrived, and that works fine when everything is already up and running, but it doesn't work when Excel is started up by double clicking the workbook.

What I'm looking for is a way to make the Addin load before the workbook, even when Excel is started up by double clicking the workbook.

xltrader100
08-06-2012, 08:48 PM
Thinking about it, I do believe your #2 above would work better than what I'm doing now. I'll give it a go and report back.

xltrader100
08-09-2012, 11:51 AM
Well, the further I went into it, the simpler it got. Finally, all it needed was to include this as the first statement after Workbook_Open
AddIns("myAddin").Installed = True

and that was enough to make the Addin load first. So thanks, Paul, you set me on the right path.

Paul_Hossler
08-09-2012, 12:04 PM
Glad it worked for you

Paul