BBB said:
Did that. Thanks. So now when I create a new spreadsheet ... how do I now
launch those macros? They are not listed in my macros list.
The AddIn needs to create its own interface -- for example, a menu or
some buttons that the user can access to run the macros.
Here's a simple example that is a slimmed-down version of an AddIn I
distribute to coworkers.
Option Explicit
Private Sub Auto_Open()
'Create the menu when the AddIn is launched.
ExampleMenu True
End Sub
Private Sub Auto_Close()
'Remove menu when the AddIn is closed.
ExampleMenu False
End Sub
Private Sub ExampleMenu(ByVal makeIt As Boolean)
Dim ctrl As CommandBarControl
Dim helpPos As Integer
Dim mac As Variant
Dim menuName, menuCapt As String
Dim i, j As Integer
'Menu constants
menuName = "Example"
menuCapt = "E&xample"
mac = Array( _
"Macro&1", False, _
"Macro&2", False, _
"Macro&3", True, _
"Macro&4", False)
'Create menu
With CommandBars("Worksheet Menu Bar")
'Delete the menu if it exists, and locate Help menu.
For Each ctrl In .Controls
If ctrl.Caption = menuCapt Then
ctrl.Delete
ElseIf ctrl.Caption = "&Help" Then
helpPos = ctrl.Index
End If
Next
If makeIt = False Then Exit Sub
'Create new menu and store it as an object.
.Controls.Add(Type:=msoControlPopup, Before:=helpPos, _
temporary:=False).Caption = menuCapt
Set ctrl = .Controls(menuName)
End With
'Add commands to menu.
For i = 0 To (UBound(mac) - 1) Step 2
With ctrl.Controls.Add(Type:=msoControlButton)
.Caption = mac(i)
.BeginGroup = mac(i + 1)
j = InStr(1, mac(i), "&")
If j > 0 Then mac(i) = Left(mac(i), j - 1) & _
Right(mac(i), Len(mac(i)) - j)
.OnAction = "Example" & mac(i)
End With
Next
End Sub
Private Sub ExampleMacro1()
MsgBox 1
End Sub
Private Sub ExampleMacro2()
MsgBox 2
End Sub
Private Sub ExampleMacro3()
MsgBox 3
End Sub
Private Sub ExampleMacro4()
MsgBox 4
End Sub