Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check for the existence of a right click menuitem ?

Status
Not open for further replies.

BobHunter

Programmer
Mar 26, 2001
62
0
0
GB
For the life of me I cannot work out how to check if a right click menu exists. I have the following code to create & delete one, but what is the code to check if it exists before I delete/create it ?

To create it I use

With Application.CommandBars("Cell").Controls.Add(Before:=1, temporary:=True)
.Caption = "Prioritise"
.OnAction = ThisWorkbook.Name & "!Prioritise"
.BeginGroup = True
End With

To delete it I use

Application.CommandBars("Cell").Controls("Prioritise").Delete

To check for it already being there ???

Any ideas ?
 
Hi BobHunter,

One way is to search the collection, perhaps with a function ..

Code:
Function ControlExists(Menu As String, Caption As String) As Boolean

    Dim ctl As CommandBarControl
    
    For Each ctl In Application.CommandBars(Menu).Controls
        If ctl.Caption = Caption Then ControlExists = True
    Next

End Function

.. and then use the function like ..

Code:
If ControlExists("Cell", "Prioritise") Then
Code:
 ' Do what you want

Enjoy,
Tony
 
this thread might be useful to you
:)

thread707-616027
 
Thanks for all your replies - I have it sorted now.
 
Thats great you got it working.

it might be helpful to others with similar questions if you post the code that you used that works.
just a thought
:)
 
Apologies, I should have reposted.

I did NOT find a way to loop through the "Cell" commandbars but rather than checking for the existence of my menu, I just reset it on the Auto_Open/Close to ensure it would not duplicate.

I know this is not the best idea as if I ever write another app that uses the right click menu, it will also wipe out these as well.

Oh well ......

Application.CommandBars("Cell").Reset
Application.CommandBars("Cell").Enabled = True
 
Try adding the following line right before your "Delete" command:

On Error Resume Next

This is what I have done with any and all Menu items I have ever created and it works for me.

I hope that this is a better solution for you.


If you can't be "The Best", be the best at what you can!!!

Never say Never!!!
Nothing is impossible!!!
 
What I meant is to write this:

On Error Resume Next
Application.CommandBars("Cell").Controls("Prioritise").Delete


This will delete your "Prioritize" Control if it is there and if it isn't it will continue to the next command (in this case "End Sub"

Good Luck!



If you can't be "The Best", be the best at what you can!!!

Never say Never!!!
Nothing is impossible!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top