MenuItem in Word/Excel Add-in

celobateira

Member
Joined
Feb 19, 2007
Messages
18
Programming Experience
Beginner
Hi,

I'm using Visual Basic 2005 to make an add-in for both Word and Excel (Office XP).

I created 1 CommandBar Button and 1 MenuItem in File Menu.

In excel both controls work perfectly, but in Word The MenuItem doesn't erase and each time I open Word, it keeps adding a new Menu Item... not to mention that the Menuitem doesn't fire any event

In other words... if I open the word 10 times, I go to the File Menu and I have 10 "MyMenuItems"

Why the add-in deletes the Menuitem for excel and not for Word?
What should I do to solve this problem?

I have this code on OnStartupComplete:

VB.NET:
MyMenuBar = applicationObject.CommandBars.Item("File")
MyMenuItem = MyMenuBar.Controls.Add(MsoControlType.msoControlButton, 1, "", 6, True)
With MyMenuItem
.Caption = "Save MyMenuItem"
.Enabled = True
.Visible = True
.OnAction = "!<MyCOMAddin.Connect>"
.Style = MsoButtonStyle.msoButtonIconAndCaption
End With
And this code on OnBeginShutDown
VB.NET:
OnErrorResumeNext
MyMenuItem.Delete(False)
MyMenuItem = Nothing
Any help Apreciated...thanx

[EDIT] Can't delete the toolbar button on word too..
 
Last edited:
I got a clue in what could be the problem...

on OnBeginShutDown Method I've used the FindControl to get the controls:

VB.NET:
[/SIZE]
[SIZE=2]MyButton = applicationObject.CommandBars.FindControl([/SIZE][SIZE=2][COLOR=#800000]"MyToolBarButton"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]MyMenuItem = applicationObject.CommandBars.FindControl([/SIZE][SIZE=2][COLOR=#800000]"MyMenuItem"[/COLOR][/SIZE][SIZE=2])[/SIZE]

[SIZE=2]

in excel it works great, but in Word it gives me an error :
'Exception from HRESULT: 0x80020005' (DISP_E_TYPEMISMATCH).

I tried to make a directcast to Microsoft.Office.Core.CommandBarButton but it didn't work.

What should I do? Please help
 
I figured out what is the problem but I don't know how to solve it...

It seems when the OnDisconnect or OnBeginShutDown events are fired, the

Word has already onloaded all the CommandBarButtons, so when

MyButton.Delete is fired it returns an error:
"Exception from HRESULT: 0x800A01A8 which (I think) means Object Required

Solution???

Maybe some event Like OnDisconnect that fires before the Word unloads the commandbarbuttons, I don't know... any help please?

PS: Makes me think... why a shared-addin to multiple office applications if we have to program each one in a different way, and if MSWord does unload the commandbars what is the purpose of the shared-addin with commandbarbuttons for Word if we can't get rid of the buttons?
 
Back
Top