Removing an added menu item

B

Bob Eaton

My COM Add-in adds a CommandBarButton to the "Edit" menu in Word. In the
call to CommandBar.Controls.Add, I pass the value "true" for the 'temporary'
parameter (I had hoped) so that the menu added would automatically remove
itself when Word exits. For some reason, that is not happening. Even if I
uninstall the add-in, the menu item
is still around.

I've even tried to explicitly call <CommandBarButton>.Delete during
Connect::OnBeginShutdown (or IDTExtensibility2.OnDisconnection), but that
doesn't seem to work either.

Any ideas?

Thanks,
Bob
 
B

Bob Eaton

Could someone at least confirm whether they've been able to create a
temporary menu CommandBarButton when adding to the built-in menu
CommandBar's (e.g. "Edit", "File", etc)?

That is, I think "temporary" works when you create a custom CommadnBar, but
not when you use an existing, built-in one.

Thanks,
Bob
 
B

Bob Eaton

I think the problem is that calling <CommandBarButton>.Delete during
OnBeginShutdown is already too late. As a test, I called it earlier (i.e.
when the button handler was called) and it successfully removed the menu.

But, now my problem is when to call it (the button handler is too soon :)

Is there an event I can add a handler for that comes when Word is shutting
down, but is *before* OnBeginShutdown?

I suppose I could add one for DocumentEvents2_CloseEventHandler, but then
I'd need to re-add my menu during DocumentEvents2_NewEventHandler, and
DocumentEvents2_OpenEventHandler...

Bob
 
P

Pete

Hi Bob,

I think the Temporary parameter of the Add method refers to the scope of the
button with regard to customization context, in other words, whether the
button persists if you load a different template. Menu items always get
cached in the current template regardless of the value of the Temporary
parameter unless they are specifically deleted before Word exits. This is why
you have to check if they exist when your add-in starts and then either
delete and re-create or add an event-handler. I don't believe this is the
case for Excel and PowerPoint.

Pete
 
B

Bob Eaton

Thanks Pete,

But I still would love a hint on how to "specifically deleted before Word
exits". OnBeginShutdown seems to be too late (it has no effect).

Thanks,
Bob
 
P

Pete

You could use the application's DocumentBeforeClose event but you would need
to ensure that the Documents.Count property is 1 if you only want to delete
menus when the last document closes. Of course if a user closes all documents
then opens a new one, your menus won't be there.

When struggling with this same problem myself, I came to the conclusion that
the best way is to allow the menus to be cached in the template then remove
them only when you uninstall the add-in. I couldn't find a 100% satisfactory
solution myself. If you find a better way, let me know!

Pete
 
D

DataDay

I've got the same situation and I think I've found the solution (actually on
this message board. It seems that Word does not respond to the Temporary
parameter and does not want to delete the button on shutdown. So I check to
see if the button exists before I add another one. I also find that I have
to remind Word about the Click event of the button.

try
{
button = (CommandBarButton)cmdbars["File"].Controls["Save
and Publish"];
button.Click += new
_CommandBarButtonEvents_ClickEventHandler(button_Click);
}
catch (System.Exception)
{
object missing = Type.Missing;

button =
(CommandBarButton)cmdbars["file"].Controls.Add(MsoControlType.msoControlButton, 1, missing,
5, true);
button.Caption = "Save and Publish";
button.Tag = "Save and Publish";
button.Click += new
_CommandBarButtonEvents_ClickEventHandler(button_Click);
}
 

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

Top