LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default trouble getting addin's workbook_open procedure to recognize activeworkbook

Hi,

My addin's Workbook_Open procedure, which runs when I load it via
Tools/Addins, does the following:

1) Adds a custom menu to the Worksheet Menu Bar
2) Adds command buttons to the custom menu
3) Disables some of those buttons unless the *active workbook* contains
a named range called "secret_word" that refers to a, well, secret word.

Trouble is, the Workbook_Open procedure doesn't seem to recognize the
active workbook (what I consider to be the workbook that was active
when I loaded the addin) as the application's activeworkbook property.
In fact, it recognizes as application.activeworkbook the just-opened
ADDIN, not the active workboook; this causes my step (3) above to
produce undesired results.

If you're still with me, create a new blank workbook, save it as
'text.xla', open up the vba editor and place the following code in
test.xla's Workbook_Open procedure. Save test.xla, then close it and
then, with a regular workbook open, load the 'Test' addin via
Tools/Addins. As it loads, you'll get a message stating that the
activeworkbook property of the application object is named 'test.xla'.
Next, Unload the addin. Now, reload it. This time the execution of the
code should stop; hit continue (F5) in the VBA editor, and you should
get a message indicating that activeworkbook is 'Book1' or whatever
workbook you have open in the Excel application.

Maybe there's a sensible explanation for why the procedure behaves
differently when execution is temporarily halted. In any case, I need a
way to grab the name of the active workbook while my addin is
opening/loading. Unless I put that Stop statement in my code (which
isn't going to work for my users) I can't seem to do it.

Private Sub Workbook_Open()

Dim FoundFlag As Boolean
Dim objStopSwitch As Name
Dim Message As String

' Search for workbook name "StopSwitch" and flag if found
FoundFlag = False
For Each nm In ThisWorkbook.Names
If nm.Name = "StopSwitch" Then
FoundFlag = True
Exit For
End If
Next

' If found, set objStopSwitch = workbook name
' If not found, set objStopSwitch = new workbook name
If FoundFlag = True Then
Set objStopSwitch = ThisWorkbook.Names("StopSwitch")
Else
Set objStopSwitch = ThisWorkbook.Names.Add _
(Name:="StopSwitch", RefersTo:="=FALSE")
ThisWorkbook.Save
End If

' The RefersTo property of objStopSwitch determines whether
' execution stops or continues
If objStopSwitch.RefersTo = "=TRUE" Then Stop

' Display message box with name of activeworkbook
MsgBox "The activeworkbook property of the application object is
named " & _
ActiveWorkbook.Name

' Toggle the RefersTo property of objStopSwitch so that next time
addin is loaded,
' the Stop statement will (or won't, depending) execute
If objStopSwitch.RefersTo = "=TRUE" Then
objStopSwitch.RefersTo = "=FALSE"
Else
objStopSwitch.RefersTo = "=TRUE"
End If

' Save addin file with new StopSwitch value
ThisWorkbook.Save

End Sub

 
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
workbook_Open event Procedure peerless Excel Discussion (Misc queries) 1 May 22nd 06 10:21 PM
workbook_Open event Procedure peerless Excel Discussion (Misc queries) 0 May 22nd 06 10:16 PM
Workbook_open procedure issue when two spreadsheets are opened Gardfd Excel Programming 2 April 6th 06 07:16 PM
Trouble with Workbook_Open() Silas Mercer Excel Programming 4 August 13th 04 03:13 PM
OnTime event not firing in Workbook_Open event procedure GingerTommy Excel Programming 0 September 24th 03 03:18 PM


All times are GMT +1. The time now is 08:19 PM.

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

About Us

"It's about Microsoft Excel"