View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
William Barnes[_2_] William Barnes[_2_] is offline
external usenet poster
 
Posts: 10
Default AddIn Name Resolution

Using Excel 2003 under Win XP Pro; all current updates installed for both
products.

I've encountered unexpected behavior with respect to name resolution between
an AddIn and a Workbook. To test Excel's behavior, I created a simple
example. I created a Workbook named MySub.xls, saved it, and then saved it
as an AddIn named MySub.xla. I then added some code to the AddIn to create a
toolbar. The entire code for both is presented. All code resides within the
respective ThisWorkbook module:

For the Workbook MySub.xls:

Public Sub MySub()
MsgBox "Workbook::MySub()"
End Sub


For the AddIn MySub.xla:

Public Sub MySub()
MsgBox "AddIn::MySub()"
End Sub

Private Sub MakeToolbar()
Dim tb As CommandBar
Dim btn As CommandBarButton

Set tb = Application.CommandBars.Add("MySub", msoBarTop)
tb.Visible = True

Set btn = tb.Controls.Add(Type:=msoControlButton)
With btn
.FaceId = 59
.OnAction = "ThisWorkbook.MySub"
.TooltipText = "MySub"
End With
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.CommandBars("MySub").Delete
End Sub

Private Sub Workbook_Open()
MakeToolbar
End Sub

As you can see, both the AddIn and the Workbook have identical Subs named
MySub. However, when I click on the tollbutton in the AddIn's toolbar, it
executes not the AddIn's version but rather the Workbook's! Thus if I write
an AddIn and a user's workbook just happens to have a Sub with the same
signature my code would not execute properly. This seems like a bona fide
bug in VBA to me. Am I missing something?