Consulting

Results 1 to 13 of 13

Thread: Remove Right-click Menu Custom Item

  1. #1
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location

    Remove Right-click Menu Custom Item

    Hello,

    I was reading a post in the Word Help forum: http://www.vbaexpress.com/forum/showthread.php?t=344

    And I have a issue with some similarities in Excel. I've successfully added two custom items to my right click menu. I've used the following code with two of my macros:


    Sub CreateRightClick1()
        With Application.CommandBars("Cell").Controls.Add
        .Caption = "Paste Values"
        .OnAction = "pasteValues"
        .Caption = "Paste Formats"
        .OnAction = "pasteFormats"
        End With
    End Sub

    My problem lies in the fact that I accidently ran this code twice and have now duplicated each control. I'm having trouble finding the correct syntax/method of removing them. Can anybody point me in the right direction here? I've seen oodles of code to add to, but taking off a custom control? Any help would be greatly appreciated, this one's exhausting me
    Last edited by Aussiebear; 04-29-2023 at 06:53 PM. Reason: Adjusted the code tags

  2. #2
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Zack: I'm asking the same question at that link: http://www.vbaexpress.com/forum/showthread.php?t=344


    We should then put all these into the kb.
    ~Anne Troy

  3. #3
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Defenitely! I was just discussing that issue with Scott. Adding/removing custom right-click controls, adjusting, etc. Very cool stuff, very useful.

  4. #4
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Btw, the code that deleted the custom controls as in regards to the above code was:


    Sub delMenu()
        Application.CommandBars("Cell").Controls("Paste Formats").Delete
        Application.CommandBars("Cell").Controls("Paste Values").Delete
    End Sub
    Last edited by Aussiebear; 04-29-2023 at 06:53 PM. Reason: Adjusted the code tags

  5. #5
    VBAX Regular
    Joined
    Jun 2004
    Location
    The Netherlands
    Posts
    34
    Location
    Hello Zack,
    tried your code but it only created one "right menu item"

    but why not first delete existing ones. like:

    Sub CreateRightClick1()
    On Error Resume Next
        Application.CommandBars("Cell").Controls("Paste Formats").Delete
        Application.CommandBars("Cell").Controls("Paste Values").Delete
    With Application.CommandBars("Cell").Controls.Add
        .Caption = "Paste Values"
        .OnAction = "pasteValues"
        End With
        With Application.CommandBars("Cell").Controls.Add
        .Caption = "Paste Formats"
        .OnAction = "pasteFormats"
        End With
    End Sub
    Last edited by Aussiebear; 04-29-2023 at 06:54 PM. Reason: Adjusted the code tags

  6. #6
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Hi Jeroen,

    Well, because I'm a lame-brain at times, to tell you the truth. I was dinking around with it and seeing what I could do. So I whipped up a couple macros and tried assigning them to righ-click contols. That worked all well and good, until I accidently ran the same macro again. Which, as you've seen and noted, doesn't take into account any of the same existing items.

    If I need to do it again, I think that's going to be the way I go with it. That's the way I've gone with any custom menus also as of lately. Avoiding duplication is a serious issue, as I've seen with 20/20 hind-sight.

    Thanks for your advice, it's greatly appreciated!!

  7. #7
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Jeroen: It'd be GREAT if you could post similar code for Word at the other link?
    ~Anne Troy

  8. #8
    BoardCoder
    Licensed Coder VBAX Expert mark007's Avatar
    Joined
    May 2004
    Location
    Leeds, UK
    Posts
    622
    Location
    I find the best way to do it is to make sure you assign the tag property of your control when you add it with something unique e.g. .Tag="AddinNameAuthor"

    You can then use the findcontrols method of the commandbar object to search for all controls with that tag and delete them.

    "Computers are useless. They can only give you answers." - Pablo Picasso
    Mark Rowlinson FIA | The Code Net

  9. #9
    Knowledge Base Approver VBAX Expert brettdj's Avatar
    Joined
    May 2004
    Location
    Melbourne
    Posts
    649
    Location
    While in this vein do we have a volunteer to write up the Disable Copy functionality etc(Menus, Icons, Keyboard shortcuts and right click)

    Jeroen?

    Cheers

    Dave

  10. #10
    In VB6, I would use a global or static boolean that gets changed when the function is run the first time. Then, everytime it is run thereafter while the application is open, it just ignores the code.

    if DidIRunTheCode = false then
    run the code
    end if

    Any reason that wouldn't work in VBA?

    ttfn
    Kicker

  11. #11
    BoardCoder
    Licensed Coder VBAX Expert mark007's Avatar
    Joined
    May 2004
    Location
    Leeds, UK
    Posts
    622
    Location
    There is no problem in principle but in practice it's not the best technique. The problem is that if you work on some other code at the same time or some other macro crashes etc. then you can lose the state of all your variables and the menu item would be added again. Also, if you closed the spreadsheet that contained the macro and later reopened it it would have it's variables reset but the toolbar item would already be there.

    "Computers are useless. They can only give you answers." - Pablo Picasso
    Mark Rowlinson FIA | The Code Net

  12. #12
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Quote Originally Posted by brettdj
    While in this vein do we have a volunteer to write up the Disable Copy functionality etc(Menus, Icons, Keyboard shortcuts and right click)

    Jeroen?

    Cheers

    Dave
    Disabling copy is actually rather difficult since there are many ways to copy. Drag & Drop for example is not really thought of as copying (at least by me) but it is.

    When I don't want people to paste I use a worksheet selection change event, then just run the code:

    Application.CutCopyMode = False
    But to specifically answer your question, this will disable the copy from the edit menu, standard toolbar, and right click menu.

    Application.CommandBars("Standard").Controls("&Copy").Enabled = False
    Application.CommandBars("Worksheet Menu Bar").Controls("&Edit").Controls("&Copy").Enabled = False
    Application.CommandBars("Cell").Controls("&Copy").Enabled = False
    Last edited by Aussiebear; 04-29-2023 at 06:57 PM. Reason: Adjusted the code tags

  13. #13
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    To get the Control + C disabled use

    Application.OnKey "^c", ""
    To Reset use

    Application.OnKey "^c"
    Last edited by Aussiebear; 04-29-2023 at 06:58 PM. Reason: Adjusted the code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •