I’ve been playing around with adding controls to the menu bar to call subs in an xla.
The idea is to add a menu to the menu bar and call the sub. One string parameter is passed.
The menu setup:
Code: Select all
Set objNewMenu = objMenuBar.Controls.Add(Type:=msoControlPopup, Temporary:=True)
objNewMenu.Caption = cMenuCaption
If sMenuID = "" Then sMenuID = GetUniqueMenuID ' tie menu to program instance
objNewMenu.Tag = sMenuID
Set objNewMenuItem = objNewMenu.Controls.Add(Type:=msoControlButton)
With objNewMenuItem
.Caption = "&Increment"
.FaceId = 329
.BeginGroup = True
.OnAction = "'D:\VBA Development\Experiments\AddInModule.xla'!Increment(""rope"")"
End With ' objNewMenuItem
Set objNewMenuItem = objNewMenu.Controls.Add(Type:=msoControlButton)
With objNewMenuItem
.Caption = "Stop!"
.FaceId = 480
.BeginGroup = False
.OnAction = "'D:\VBA Development\Experiments\AddInModule.xla'!stopper"
End With ' objNewMenuItem
Code: Select all
Private slCounter As Long
Public Sub Increment(sAction As String) ' increment counters;
' document workbook name/counter values to immediate
Dim wbCaller As Workbook
Static slSubCounter As Long
Debug.Assert False
Set wbCaller = ActiveWorkbook
slCounter = slCounter + 1
slSubCounter = slSubCounter + 1
Debug.Print sAction; " "; wbCaller.Name, "Counter="; slCounter, "Sub Counter="; slSubCounter
End Sub ' Increment
Public Sub Stopper()
Debug.Assert False
End Sub ' Stopper
It does that, then it prints a second line!
The sub is obviously being executed twice, but I can't figure out how or why!rope TestBook3.xls Counter= 1 Sub Counter= 1
rope TestBook3.xls Counter= 2 Sub Counter= 2
The Debug.Assert False I put in to halt execution, so I could look at what was going on, is ignored.
Stopper works as expected...Click the control, execution halts in the sub.
Any explanations or insights appreciated in advance.
Regards,
ZK