Office 2007中VBA开发遇到的问题
这是个右键菜单的函数。运行到oPopup.ShowPopup时会弹出如下错误。请问这是怎么回事?谢谢!
Public Sub ShowRightMenue()
Dim oPopup As CommandBar
Dim oSubMenu As CommandBarPopup
Dim oMenuItem As CommandBarControl
Cancel = True
On Error Resume Next
CommandBars("myRightMenu").Delete
On Error GoTo 0
Set oPopup = CommandBars.Add("myRightMenu", msoBarPopup)
Set oSubMenu = oPopup.Controls.Add(msoControlPopup)
oSubMenu.Caption = "一级子菜单1"
Set oMenuItem = oSubMenu.Controls.Add(msoControlButton)
oMenuItem.Caption = "菜单项1-1"
Set oMenuItem = oSubMenu.Controls.Add(msoControlButton)
oMenuItem.Caption = "菜单项1-2"
Set oMenuItem = oSubMenu.Controls.Add(msoControlButton)
oMenuItem.Caption = "菜单项1-3"
Set oSubMenu = oPopup.Controls.Add(msoControlPopup)
oSubMenu.Caption = "一级子菜单2"
Set oMenuItem = oSubMenu.Controls.Add(msoControlButton)
oMenuItem.Caption = "菜单项2-1"
Set oMenuItem = oSubMenu.Controls.Add(msoControlButton)
oMenuItem.Caption = "菜单项2-2"
Set oMenuItem = oSubMenu.Controls.Add(msoControlButton)
oMenuItem.Caption = "菜单项2-3"
oPopup.ShowPopup
oPopup.Delete
End Sub
[解决办法]
把
oPopup.ShowPopup
改成
Application.CommandBars("myRightMenu").ShowPopup
看看