CommandBarButton testMenu = ExcelCommandBarUtil.GetCommandBars()["cell"].Controls.AddButton();
testMenu.OnAction = @"FooAction(""Hello, World"")";
testMenu.Caption = "Foo";
public static void FooAction(string s)
{
MessageBox.Show(s);
}
Sub InitMenus()
For Each ctl In CommandBars("Cell").Controls
If InStr(1, ctl.Caption, "TEST") = 1 Then
ctl.Delete
End If
Next ctl
Set btn = CommandBars("Cell").Controls.Add(msoControlButton)
btn.OnAction = "FooAction(""Hello, World"")"
btn.Caption = "TEST FooAction(""Hello, World"")"
'Result: MsgBox pops up twice w/Hello followed by "can't run macro Foo Action..."
Set btn = CommandBars("Cell").Controls.Add(msoControlButton)
btn.OnAction = "'FooAction(""Hello, World"")'"
btn.Caption = "TEST 'FooAction(""Hello, World"")'"
'Result: "can't run macro FooAction..."
Set btn = CommandBars("Cell").Controls.Add(msoControlButton)
btn.OnAction = "LocalFooAction(""Hello, World"")"
btn.Caption = "TEST LocalFooAction(""Hello, World"")"
'Result: MsgBox pops up twice w/Hello
Set btn = CommandBars("Cell").Controls.Add(msoControlButton)
btn.OnAction = "'LocalFooAction(""Hello, World"")'"
btn.Caption = "TEST LocalFooAction(""Hello, World"")"
'Result: Success!
Set btn = CommandBars("Cell").Controls.Add(msoControlButton)
btn.OnAction = "'LocalFooAction ""Hello, World""'"
btn.Caption = "TEST 'LocalFooAction ""Hello, World""'"
'Result: Success!
Set btn = CommandBars("Cell").Controls.Add(msoControlButton)
btn.OnAction = "'FooAction ""Hello, World""'"
btn.Caption = "TEST 'FooAction ""Hello, World""'"
'Result: "can't run macro "c\...\Book2.xlsm'!'FooAction..."
End Sub
Sub LocalFooAction(s As String)
MsgBox (s)
End Sub
Sub LocalFooAction(s As String)
Call Application.Run("FooAction", s)
End Sub