ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Adding menu to the mouse right click pop-up menu (https://www.excelbanter.com/excel-programming/291129-adding-menu-mouse-right-click-pop-up-menu.html)

jack

Adding menu to the mouse right click pop-up menu
 
Is it possible to add customized menu to the mouse right click pop-up menu
and attach script to be executed with it?



Robert Rosenberg[_2_]

Adding menu to the mouse right click pop-up menu
 
This example adds a Custom command to the Cell shortcut menu when the
workbook the code is in opens. It then removes it when closed:

FIRST, copy and paste (and modify as desired) the routines listed below in
the ThisWorkbook object (in the VBE) of the desired workbook:

__________________________________________

Const mszTAG As String = "C&ustom Command" 'Is the name that appears on
the shortcut menu

Private Sub Workbook_Open()


Dim cbr As CommandBar
Dim ctl As CommandBarControl

Set cbr = Application.CommandBars("Cell")
'Add your custom command to the cells shortcut menu
With cbr
Set ctl = .Controls.Add(msoControlButton) 'By default, the
control appears at the bottom of the menu
With ctl
.BeginGroup = True 'This adds a separator before the custom
control
.Caption = mszTAG 'This is what appears on the Right-Click
(shortcut) menu
.Tag = mszTAG 'Assigning mszTAG to this property allows us to
search for and delete the command when the workbook closes
.OnAction = "CustomCommand" 'Is the name of the macro that
runs when you click the custom command
End With
End With

End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Remove it
On Error Resume Next
'Find and delete the control on the Right-Click menu (called the Cell
commandbar) based on
' the text found in the control's .Tag property, which is mszTAG.
Application.CommandBars("Cell").FindControl(, , mszTAG).Delete
End Sub

__________________________________________

SECOND, in a regular module, place the following routine, which is the macro
the custom command will run when it's clicked:

Sub CustomCommand()
'Access the control clicked (called the ActionControl) and display it's
Caption in a message box
MsgBox CommandBars.ActionControl.Caption
End Sub

--
_______________________
Robert Rosenberg
R-COR Consulting Services
Microsoft MVP - Excel


"Jack" <replyto@newsgroup wrote in message
...
Is it possible to add customized menu to the mouse right click pop-up menu
and attach script to be executed with it?






All times are GMT +1. The time now is 06:24 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com