Excel VBA: Clear the 'Cell' context menu and all items
Requirement:
I want to clear all items on the Cell context menu using VBA.
Objective:
I am adding my own 'paste command' to the cell menu and want to remove all other commands and means of paste via the cell menu.
Context:
I have used Ribbon X to add a new custom item to the cell menu and I want to clear all other items - ICLUDING the 'Paste Options' sub-menu as below:
Current progress:
I have previously asked on EE and found that there is no RibbonX way (PREFERRED) to remove all items on the context menu (a very big omission on the part of MS) and that the only remaining option is to use the now depreciated 'CommandMenu' control objects to remove all builtin items. This is done as follows:
Sub removeCellMenuItems() Dim cbcCurControl As CommandBarControl For Each cbcCurControl In CommandBars("Cell").Controls If cbcCurControl.BuiltIn Then cbcCurControl.Visible = False Next cbcCurControlEnd Sub
This of course works but leaves behind the annoying 'Paste Options' submenu. How can I get rid of this ?
Microsoft ExcelVisual Basic ClassicMicrosoft DevelopmentMicrosoft OfficeOffice Suites-Other
Last Comment
AL_XResearch
8/22/2022 - Mon
AL_XResearch
ASKER
Please note I have found articles such as the below but despite turning off all three 'Cut, copy & paste' options (and restarting Excel - just in case) this 'Paste Options' floating gallery is still there !
you can get what you want using VBA, hardway will be to run the below code and get list of all shortcut menus and then incorporate it into the VBA code.
Sub ListShortCutMenus() Dim r As Long Dim cBar As CommandBar Dim c As Integer r = 1 For Each cBar In CommandBars If cBar.Type = msoBarTypePopup Then Cells(r, 1) = cBar.Index Cells(r, 2) = cBar.Name For c = 1 To cBar.Controls.Count Cells(r, c + 2) = cBar.Controls(c).Caption Next c r = r + 1 End If Next cBarEnd Sub
easy way will be using the free Add-in once loaded the add-in then it will appear in the developer tab then click on the Menurighter add-in icon and then once it is open, on the "target" drop down there are to "cell" select the first one, the from the left icon "remove" or by double click remove all of the menus and leave only the last one which is "&Additional Actions" then from the dropdown of "target" select the second "cell" and do the same thing remove all and leave only "&Additional Actions" then close add-in. you will see that your right click will have nothing, not even the paste special gallery or menu.
let me know how it goes.
AL_XResearch
ASKER
ProfessorJimJam: Yes I have already tried this but as I say it does not remove 'Paste options' indeed that 'gallary' style control is not even listed when you output all control captions for the "Cell" menu to a worksheet.
Clearing all controls in the "Cell" menu does not clear the "Paste options" control.
All this is shown in the picture below
This still leaves me looking for a solution
Professor J
can you try like this? it does remove all including the gallery of paste .
ProfessorJimJam: That is very interesting. My first though when I saw your latest example was "that won't work - I have tried that before and it left the 'Paste Options' control behind" but it did work.
It appears that if you set each control to ".visible = False" then 'Paste Options' remain but if you 'Delete' them it clears all.
You mentioned you have already given the RibbonX for this in another post but I don't think that was me as I have been asking this on EE in various forms for some time now and nobody has yet solved it (apart from yourself now :) )
I would really appreciate you given the RibbonX if there is any. Cheers.
Unfortunately when I tried the RibbonX that you supplied (ID: 40785604) it did not remove all the items in the conext menu as shown below.
Professor J
is this case, then please use the VBA solution on this thread.
AL_XResearch
ASKER
I want to keep away from the CommandBar objects since a) I don't know when they will be depreciated by MS b) I don't want to mix interface specification between RibbonX and VBA
So you are saying there is no RibbonX way to do what I want as far as you know ?
I would also point out that with RibbonX the user interface adjustments are automatically revoked & restored when you switch workbooks / sessions whereas with VBA you have to be careful to implement this yourself.
Professor J
there is possibility to do this, but it require a bit of time to manipulate. for example apart from the commands idMSOs
you need to find the contextual menus for the paste menus to disable.
Turning Off Paste Options