VBA - Remove almost all right click context menu options

kgkev

Well-known Member
Joined
Jun 24, 2008
Messages
1,291
Office Version
  1. 365
Platform
  1. Windows
I have removed most of them, I just want to keep "Copy" and then add my own macros to the right click menu.

However I have not been able to remove Paste options or Link

Any ideas what the correct name for these components is?
Or maybe a neater way to "removed all" <> Copy


Code:
Private Sub Workbook_Open()
    Application.CommandBars("Cell").Reset
    Application.CommandBars("Cell").Controls("Cut").Delete
    Application.CommandBars("Cell").Controls("Smart Lookup").Delete
    Application.CommandBars("Cell").Controls("Translate").Delete
    Application.CommandBars("Cell").Controls("Paste Special...").Delete
    Application.CommandBars("Cell").Controls("Insert...").Delete
    Application.CommandBars("Cell").Controls("Delete...").Delete
    Application.CommandBars("Cell").Controls("Clear Contents").Delete
    Application.CommandBars("Cell").Controls("Quick Analysis").Delete
    Application.CommandBars("Cell").Controls("Filter").Delete
    Application.CommandBars("Cell").Controls("Sort").Delete
    Application.CommandBars("Cell").Controls("Insert Comment").Delete
    Application.CommandBars("Cell").Controls("Format Cells...").Delete
    Application.CommandBars("Cell").Controls("Pick From Drop-Down List...").Delete
    Application.CommandBars("Cell").Controls("Define Name...").Delete
    'Application.CommandBars("Cell").Controls("Link").Delete
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
try one of these:
PasteMenu
PasteGallery
PasteGalleryMini
 
Upvote 0
Code:
Application.CommandBars("Cell").Controls("PasteMenu").Delete
Application.CommandBars("Cell").Controls("PasteGallery").Delete
Application.CommandBars("Cell").Controls("PasteGallerymini").Delete
thanks for the suggestions, unfortunately none of them worked.
I created a routine that gets the caption for each context menu item
Code:
Sub getname()
For Each Item In Application.CommandBars("Cell").Controls
    MsgBox Item.Caption
Next Item
End Sub

I managed to find the paste one...

Code:
Application.CommandBars("Cell").Controls("paste").Delete

but still can't identify "link"
 
Last edited:
Upvote 0
found it....


Code:
Application.CommandBars("Cell").Controls("hyperlink...").Delete
 
Upvote 0
Sorry, I was thinking idMSO and Ribbon-wise :)
However I think another way to do it instead of trying to figure each name:

Code:
Sub ClearContMenuButCopy()
Dim item1  As CommandBarControl
    On Error Resume Next
    For Each item1 In Application.CommandBars("Cell").Controls
        With item1
            If Replace(.Caption, "&", "") <> "Copy" Then .Delete
        End With
    Next item1
End Sub
 
Upvote 0
bobsan42

[FONT=&quot] Hello there! Can you help me too?
How can I move the button "Keep original column width" from 'Paste special' to "The paste options" ?

[/FONT]
poks5y
 
Upvote 0
At the moment this is the closest I can get to what you want to achieve:
Code:
Application.CommandBars("Cell").Controls.Add msoControlButton,19086,,4

Below is a code that can list the controls and commandbars (start it against a blank sheet).
Code:
Sub getNamesCommandbars()
    On Error Resume Next
    Dim cb As CommandBar, cbcon As CommandBarControl, cbm As CommandBarPopup
    Dim rng As Range: Set rng = ActiveSheet.Range("D1") 'Activecell
    rng.Resize(1, 11) = Split("index,command bar name,type,context,controls,ID,Caption,Tag,TypeName,Tooltip,Type", ",")
    Set rng = rng.Offset(1)


    For Each cb In Application.CommandBars
        With rng
            .Value = cb.Index
            .Offset(, 1).Value = cb.Name
            .Offset(, 2).Value = cb.Type
            .Offset(, 3).Value = cb.Parent
            .Offset(, 4).Value = cb.Controls.Count
        End With
        Set rng = rng.Offset(1)
        For Each cbcon In cb.Controls
            With rng
                .Offset(, 4).Value = cbcon.Index
                .Offset(, 5).Value = cbcon.ID
                .Offset(, 6).Value = cbcon.Caption
                .Offset(, 7).Value = cbcon.Tag
                .Offset(, 8).Value = TypeName(cbcon)
                .Offset(, 9).Value = cbcon.TooltipText
                .Offset(, 10).Value = cbcon.Type
                .Offset(, 11).Value = cbcon.DescriptionText
            End With
            Set rng = rng.Offset(1)
        Next cbcon
    Next cb
    Set rng = Nothing
    Set cb = Nothing
    Set cbcon = Nothing
End Sub
 
Upvote 0
Might be a "Dirty" work around....but how about recreating the action of the "Keep column Width" in a new macro.

Then adding that control to the right click menu.

This is what mine looks like now:


Code:
Private Sub Workbook_Open()


Application.CommandBars("Cell").Reset   'resets menu
For Each Item In Application.CommandBars("Cell").Controls    'removes all right click menu items
    Item.Delete
Next Item
    
    With Application.CommandBars("Cell").Controls.Add(Temporary:=True)
        .Caption = "Main Update"
        .Style = msoButtonCaption
        .OnAction = "Main_Update"
    End With
[U][B]*Repeat adding menu items.....[/B][/U] 
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top