Help with creating menu item for Addin

BAlGaInTl

Well-known Member
Joined
May 7, 2003
Messages
1,082
I know that this has been covered before, and I have a working macro that creates a menu item in order to run a macro. Here is the code that I'm currently using...

Code:
Private Sub CreateMenu()
    Dim NewMenu As CommandBarPopup
            Set NewMenu = CommandBars("Worksheet Menu Bar").Controls.Add _
                (Type:=msoControlPopup)
    With NewMenu
        .Caption = "QAD"
        .OnAction = "ShowQADForm"
    End With
End Sub

I want this macro to automatically run when the Addin is installed. However, when I create this same macro under Workbook_AddinInstall, I get an error.

Code:
Run-time error '91':
     Object variable or with block variable not set

The Set NewMenu line is highlighted when I debug. What do I need to change to get it to work automatically?

Also, I would like to create the item before the 'Help' menu item, but all of the Before:= statements that I have tried up to this point have failed. That is by no means a show stopper, it would just be nice.

Any help is appreciated.

TIA
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hi sdible,

You need to add the Application object to your CommandBars.
The intIndex variable stores the position of the Help menu item.
Code:
    Dim NewMenu As CommandBarPopup
    Dim intIndex As Integer
    
    intIndex = Application.CommandBars("Worksheet Menu Bar").FindControl(ID:=30010).Index
    Set NewMenu = Application.CommandBars("Worksheet Menu Bar").Controls.Add _
                (Type:=msoControlPopup, Before:=intIndex)
    With NewMenu
        .Caption = "QAD"
        .OnAction = "ShowQADForm"
    End With

Cheers
Andy
 
Upvote 0
Thanks so much... that works like a charm..

However... (of course that was coming)

I thought I could figure out how to set up the AddinUninstall code in order to remove the custom menu when the Addin in Uninstalled.

I can do it by the index number used on my machine, but I think it will be different on others.

How can I set up the intIndex = command to find my new custom "QAD" menu so I can delete it?

TIA
 
Upvote 0
This should do it.

Application.CommandBars("Worksheet Menu Bar").Controls("QAD").Delete

Cheers
Andy
 
Upvote 0
I tried that...

I don't think it worked, but I will try again.

Also...

Hopefully you can help me... in playing around with my stuff, I inadvertently deleted the Help menu.

Is there a quick way to get it back?

[edit]
It seems to work now... I must have had a typo in there before. I hate stupid mistakes.

I also managed to add the Help menu back in.
[/edit]

Everything works like a dream now. Thanks for your help. (y)
 
Upvote 0

Forum statistics

Threads
1,214,873
Messages
6,122,029
Members
449,061
Latest member
TheRealJoaquin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top