VBA Run-time error '5' Invalid procedure or arguement

  • Thread starter Thread starter kraljb
  • Start date Start date
K

kraljb

I have a macro that adds a menu button to excel, but it has bee
erroring out now. Here is the code that is being effected:

Private Sub Workbook_Open()
Dim MenuName as CommandBarButton
Dim ButtonName as CommandBarPopup

If Not CheckMenu("MenuName") Then
Set ButtonName = Application.CommandBars("Worksheet Men
Bar").Controls.Add(msoControlPopup, , , , True)
ButtonName.Caption = "MenuN&ame"
Else
Set ButtonName = Application.CommandBars("Worksheet Men
Bar").Controls.Item("MenuName")
End If
Set MenuName = ButtonName.Controls.Add(msoControlButton, , , , True)
MenuName.caption = "Report1"
MenuName.OnAction = "RunReport1"
MenuName.BeginGroup = True
End Sub

Function CheckMenu(caption As String) As Boolean
Dim ButtonName as CommandBarPopup
-On Error GoTo nomenu-
CheckMenu = False
*Set ButtonName = Application.CommandBars("Worksheet Men
Bar").Controls.Item(caption)*
CheckMenu = True
-nomenu:-
End Function

The error is occuring on the *bold* line. I am not very good with code
but shouldn't the Invalid procedure or argument be handled with th
error handler -Italics-.

What I do not understand at all is that it was working, and now it i
not, but there has been no change to the code. Although there was a
upgrade to a proprietary software happening when the change occured
Could this be related to that, or just a fluke?


Thank you in advance,
Joh
 
After I adjusted the linewrap problems, your code worked ok for me in xl2002.

Excel seemed to work ok with me using a parameter named caption, but I wouldn't
use this. It's confusing to things like:
buttonname.caption

(But again, xl2002 forgave me and ran the code fine.)
 

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

Back
Top