在Excel(2010)中,我创建了一个菜单
Dim NewMenu As CommandBarPopup
Set NewMenu = CommandBars(1).Controls.Add(Type:=msoControlPopup, Before:=HelpMenu.Index, temporary:=True)
NewMenu.Caption = "&MyMenu"
并在其中添加了一些项目
Dim MenuItem As CommandBarControl
Set MenuItem = NewMenu.Controls.Add(Type:=msoControlButton)
With MenuItem
.Caption = "&Item1..."
.Tag = "Item1"
End With
现在稍后,如何找到具有VBA的特定菜单项,以启用或禁用它?例如?
我尝试了下面的
Dim itm As CommandBarControl
For Each itm In Application.CommandBars(1).Controls("&MyMenu")
If itm.Tag="Item1" Then ...
Next
但是“对象不支持此类型或方法”出现错误。
答案 0 :(得分:3)
无需循环,您可以直接解决Item
。例如
Dim ctlItm As CommandBarControl
Set ctlItm = NewMenu.Controls("Item1...")
答案 1 :(得分:1)
尝试使用以下代码。它对我有用:
Dim itm As CommandBarControl
For Each itm In Application.CommandBars(1).Controls("&MyMenu").Controls
If itm.Tag = "Item1" Then
'do something
End If
Next