Const strMacro = "YourCode" Sub CreateMacro() Dim cBut Call KillMacro Set cBut = Application.CommandBars("Cell").Controls.Add(Temporary:=True) With cBut .Caption = strMacro .Style = msoButtonCaption .OnAction = "Test_Macro" End With End Sub Sub Test_Macro() MsgBox "I work" End Sub Sub KillMacro() On Error Resume Next Application.CommandBars("Cell").Controls(strMacro).Delete End Sub
cRightClickEventHandler
Public WithEvents oApp As Application Private Sub Class_Initialize() Set oApp = Application End Sub Private Sub Class_Terminate() Set oApp = Nothing End Sub Private Sub oApp_WindowBeforeRightClick(ByVal Sel As Selection, Cancel As Boolean) CreateCommand Cancel = True End Sub
Option Explicit Public Const sCommName As String = "Custom" Public cApp As cRightClickEventHandler Sub CreateCommand() Dim cb As CommandBar Dim ctr As CommandBarControl On Error GoTo Err_CreateCommand KillCommand Set cb = Application.CommandBars.Add(sCommName, msoBarPopup) Set ctr = cb.Controls.Add(msoControlButton) With ctr .Caption = "Let's do it!" .TooltipText = .Caption .FaceId = 611 .OnAction = "SayHello" End With cb.ShowPopup Exit_CreateCommand: On Error Resume Next Set ctr = Nothing Set cb = Nothing Exit Sub Err_CreateCommand: MsgBox Err.Description, vbExclamation, Err.Number Resume Exit_CreateCommand End Sub Sub KillCommand() On Error Resume Next Application.CommandBars(sCommName).Delete End Sub Sub SayHello() MsgBox Selection.Text, vbInformation, "Hello..." End Sub
ThisDocument
Option Explicit Private Sub Document_Close() Set cApp = Nothing End Sub Private Sub Document_Open() Set cApp = New cRightClickEventHandler End Sub
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)