Delete specific menu item from custom menu

T

Tim

I am creating a new menu called STI to be used by future add-ins. Within
that menu, I added a menu item(msControlButton), Format Aging Report, to run
a macro. How do I delete Format Aging Report without deleting STI menu?

Sub AddMenus()

Dim cbMainMenuBar As CommandBar
Dim HelpMenuIndex As Integer
Dim cbcSTIMenu As CommandBarControl

'Would like to try to delete "Format Aging Report" here instead of current
code
On Error Resume Next
Application.CommandBars("Worksheet Menu Bar").Controls("&STI").Delete
On Error GoTo 0

Set cbMainMenuBar = Application.CommandBars("Worksheet Menu Bar")

HelpMenuIndex = cbMainMenuBar.Controls("Help").Index

Set cbcSTIMenu = cbMainMenuBar.Controls.Add(Type:=msoControlPopup,
Before:=HelpMenuIndex)

cbcSTIMenu.Caption = "&STI"

With cbcSTIMenu.Controls.Add(Type:=msoControlButton)
.Caption = "Format Aging Report"
.OnAction = "AgingReportFormat"
End With

End Sub
 
T

Tim

Thank you Dom, here is what I came up with.
(Any suggestions on error handling or formatting welcomed)

Dim cbMainMenuBar As CommandBar
Dim HelpMenuIndex As Integer
Dim cbcSTIMenu As CommandBarControl
Dim cbcAgingReport As CommandBarControl

Set cbMainMenuBar = Application.CommandBars("Worksheet Menu Bar")
Set cbcSTIMenu = cbMainMenuBar.FindControl(Type:=msoControlPopup,
Tag:="STI")

On Error Resume Next
cbcSTIMenu.Controls("Format Aging Report").Delete
On Error GoTo 0

If cbcSTIMenu Is Nothing Then

HelpMenuIndex = cbMainMenuBar.Controls("Help").Index

Set cbcSTIMenu = cbMainMenuBar.Controls.Add(Type:=msoControlPopup,
Before:=HelpMenuIndex)
cbcSTIMenu.Caption = "&STI"
cbcSTIMenu.Tag = "STI"
End If

With cbcSTIMenu.Controls.Add(Type:=msoControlButton)
.Caption = "Format Aging Report"
.OnAction = "AgingReportFormat"
End With
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top