Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Trouble with modifying cell right-mouse-click context menu

Question relates to modifying the right-mouse-click cell context menu in
Excel 2007

I am attempting to add a menu item to the cell context menu that pops up
upon clicking the right mouse button. I found a microsoft article he
http://msdn.microsoft.com/en-us/library/bb211466.aspx that seems to explain
how to do this. Based on this article, I have inserted VBA code into the
worksheet of interest as follows:

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, _
Cancel As Boolean)
For Each icbc In Application.CommandBars("cell").Controls
If icbc.Tag = "RollJob" Then icbc.Delete
Next icbc

With Application.CommandBars("cell").Controls _
.Add(Type:=msoControlButton, befo=6, _
temporary:=True)
.Caption = "Roll Job"
.OnAction = "RollJob"
.Tag = "RollJob"
End With

End Sub

Private Sub Worksheet_Deactivate()

For Each icbc In Application.CommandBars("cell").Controls
If icbc.Tag = "RollJob" Then icbc.Delete
Next icbc

End Sub

RollJob is a macro in a module within the same workbook.

When I right-click in this sheet, nothing in the context menu appears to
have changed. Can anyone figure out what I am doing wrong here?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default Trouble with modifying cell right-mouse-click context menu

Try running this when you want to reset the command bars back to the default
settings.

Sub ResetCommandBars()

Application.CommandBars("Cell").Reset
Application.CommandBars("Row").Reset
Application.CommandBars("Column").Reset

End Sub
--
HTH,

Barb Reinhardt



"jlwilloz" wrote:

Question relates to modifying the right-mouse-click cell context menu in
Excel 2007

I am attempting to add a menu item to the cell context menu that pops up
upon clicking the right mouse button. I found a microsoft article he
http://msdn.microsoft.com/en-us/library/bb211466.aspx that seems to explain
how to do this. Based on this article, I have inserted VBA code into the
worksheet of interest as follows:

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, _
Cancel As Boolean)
For Each icbc In Application.CommandBars("cell").Controls
If icbc.Tag = "RollJob" Then icbc.Delete
Next icbc

With Application.CommandBars("cell").Controls _
.Add(Type:=msoControlButton, befo=6, _
temporary:=True)
.Caption = "Roll Job"
.OnAction = "RollJob"
.Tag = "RollJob"
End With

End Sub

Private Sub Worksheet_Deactivate()

For Each icbc In Application.CommandBars("cell").Controls
If icbc.Tag = "RollJob" Then icbc.Delete
Next icbc

End Sub

RollJob is a macro in a module within the same workbook.

When I right-click in this sheet, nothing in the context menu appears to
have changed. Can anyone figure out what I am doing wrong here?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Trouble with modifying cell right-mouse-click context menu

I'm not sure why you are recreating the control each time the
BeforeRightClick event fires. Even still, the following code works for
me:

Private Sub Worksheet_BeforeRightClick( _
ByVal Target As Range, Cancel As Boolean)
Dim C As Office.CommandBarControl
Const C_TAG = "MyTag"
Set C = _
Application.CommandBars("Cell").FindControl(Tag:=C _TAG)
Do Until C Is Nothing
C.Delete
Set C = _
Application.CommandBars("Cell").FindControl(Tag:=C _TAG)
Loop
With Application.CommandBars("Cell").Controls.Add( _
Type:=msoControlButton, befo=6, temporary:=True)
.Caption = "Click Me"
.OnAction = "'" & ThisWorkbook.Name & "'!ClickMe"
.Tag = C_TAG
End With
End Sub


where "ClickMe" is a sub proc defined in a regular code module. I
would build the control once in the sheet's Activate event and tthen
enable it in Activate and disable it in the Deactivate event. Don't
put any code in the BeforeRightClick procedure.

Private Const C_TAG = "MyTag"

Private Sub Worksheet_Activate()
Dim C As Office.CommandBarControl
Set C = _
Application.CommandBars("Cell").FindControl(Tag:=C _TAG)
If C Is Nothing Then
With Application.CommandBars("Cell").Controls.Add( _
Type:=msoControlButton, befo=6, temporary:=True)
.Caption = "Click Me"
.OnAction = "'" & ThisWorkbook.Name & "'!ClickMe"
.Tag = C_TAG
End With
End If
C.Enabled = True
End Sub


Private Sub Worksheet_Deactivate()
Dim C As Office.CommandBarControl
Set C = Application.CommandBars("Cell").FindControl(Tag:=C _TAG)
If Not C Is Nothing Then
C.Enabled = False
End If
End Sub


This code creates the menu item the first time the sheet is activated,
and on subsequent activations simply enables tthe existing control.
When the sheet is deactivated, the control is disabled rather than
deleted. If you don't want to leave the control visible when the sheet
is deactivated, add

C.Visible = False

to the Deactivate event and

C.Visible = True

to the Activate event.


There is no reason to delete and recreate the control every time
BeforeRightClick runs. Create the ccontrol once and be done with it.

Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
[email on web site]








On Fri, 12 Mar 2010 09:24:01 -0800, jlwilloz
wrote:

Question relates to modifying the right-mouse-click cell context menu in
Excel 2007

I am attempting to add a menu item to the cell context menu that pops up
upon clicking the right mouse button. I found a microsoft article he
http://msdn.microsoft.com/en-us/library/bb211466.aspx that seems to explain
how to do this. Based on this article, I have inserted VBA code into the
worksheet of interest as follows:

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, _
Cancel As Boolean)
For Each icbc In Application.CommandBars("cell").Controls
If icbc.Tag = "RollJob" Then icbc.Delete
Next icbc

With Application.CommandBars("cell").Controls _
.Add(Type:=msoControlButton, befo=6, _
temporary:=True)
.Caption = "Roll Job"
.OnAction = "RollJob"
.Tag = "RollJob"
End With

End Sub

Private Sub Worksheet_Deactivate()

For Each icbc In Application.CommandBars("cell").Controls
If icbc.Tag = "RollJob" Then icbc.Delete
Next icbc

End Sub

RollJob is a macro in a module within the same workbook.

When I right-click in this sheet, nothing in the context menu appears to
have changed. Can anyone figure out what I am doing wrong here?

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Right click menu (context menu) for Rows & col not working Madiya Excel Programming 5 December 26th 09 10:07 AM
Screwed Up my Right Click Context Menu, Please Help cpmame Excel Programming 11 March 12th 09 10:59 PM
Undo button in the context menu (right click menu) Madiya Excel Programming 7 April 7th 08 04:33 PM
Custom Context menu (Right click menu) not working in sheet changeevent. Madiya Excel Programming 3 February 11th 08 01:24 PM
Right-Click (Context Menu) Kryptonix Excel Programming 6 October 25th 05 03:59 PM


All times are GMT +1. The time now is 06:42 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"