Share via

Run Time error 5 when running commandbar in 2003

Anonymous
2012-08-18T21:14:17+00:00

I am new to vba so I'm not sure what the error messages mean, but I keep getting this "run-time error '5': invalid procedure call or argument" whenever I run the macro.  I select debug and found that this line is causing the error - "Set mybar = Application.CommandBars.Add(Name:="My Macro", Position:=msoControlButton, Temporary:=True)", but I can' figure out what is wrong with it.  It seems to execute this line even after the error message appears.  Here is the entire code:

Dim mybar As Object, newcontrol As Object

Set mybar = Application.CommandBars.Add(Name:="My Macro", Position:=msoControlButton, Temporary:=True)

Set newcontrol = mybar.Controls.Add(Type:=msoControlButton, ID _

        :=2950, Before:=1)

newcontrol.OnAction = "reducetime"

mybar.Visible = True

It's suppose to create a custom command button in the ribbons in excel 2003, and it actually works but the error always appears when it executes.

Thanks,

Nate

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

11 answers

Sort by: Most helpful
  1. Anonymous
    2012-08-25T12:17:39+00:00

    The other option is that you are trying to add a control that already exists??

    Add code to delete old toolbars or at least an On Error Resume Next

    Before the lines to add command bar

    On Error Resume Next 'in case it's not there

    Application.CommandBars("MyMacro").Delete

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2012-08-24T00:00:27+00:00

    Do you have another link?  I can not view with my browser.

    Was this answer helpful?

    0 comments No comments
  3. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2012-08-22T07:16:33+00:00

    Download this AddIn:

    http://dl.dropbox.com/u/35239054/CSV.xla

    Look into the code module "DieseArbeitsmappe" into Sub MakeMenu.

    See this as working example. It creates temporary items in the file menu and context menu of the cells (right click a cell).

    Andreas.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2012-08-22T03:46:47+00:00

    I try using the Position arguments that you suggested but I'm still getting the same error when I run my macro.

    Was this answer helpful?

    0 comments No comments
  5. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2012-08-20T10:14:29+00:00

    The type of argument Position is MsoBarPosition, you try to use a constant from MsoControlType.

    Open the VBA editor, read the help to CommandBars.Add, especially on the argument Position.

    In the VBA editor,press F2 to open the object catalog, enter "MsoBarPosition" in the second combobox from above and click the search button.

    Andreas.

    Was this answer helpful?

    0 comments No comments