Deleting CommandBarButton menu item.

D

David

I'm trying to add a menu item to MS Word under "File -> Send To." I've
written some code in C# from the code generated by the .NET 2003 Add-in
wizard.
My class uses the IDTExtensibility2 interface.

I've successfully created a menu bar item under the Send To popup of the
File menu bar. When I uninstall the add-in, the menu bar stays and does
nothing, which is not desirable. I would like to remove the menu item when
the add-in is uninstalled. I figured the way to do this would be to delete
the menu item when MS Word shuts down - that way it must be reloaded, so if
there is no add-in, there is no menu item.

I've tried many different ways of deleting the menu item, but either I
receive an unhandled COMException HRESULT=0x800A01A8 when attempting to
delete it. I've tried deleting in OnBeginShutdown(), OnDisconnection() and
as an event handler to ApplicationEvents#_Event_Quit (where #=2,3). Nothing
has worked.

I think the problem is that any changes made at this point are not persisted
by Word. If I add a menu item, or change a property during shutdown, those
changes do not make it in.

If anybody has any ideas, I would greatly appreciate them.
Thanks,
David
 
D

David Thielen

Hi;

I have spent days on this same problem and have come to the conclusion that
it cannot be done. The changes go in normal.dot (unless you set another
template as the CustomizationContext) and by the time the close events hit,
it has been saved and so you can't change it.

sorry - dave
 
K

Ken Slovak - [MVP - Outlook]

Document.Close should work. You can also set
Application.CustomizationContext.Saved = True or
ActiveDocument.AttachedTemplate.Saved = True. Either or both of those should
work to prevent saving the change to Normal.dot. You set those properties
just after you create the button to fool Word into not saving Normal.dot.
 
D

David

Thanks for those ideas. I was starting to think that this was hopeless.
So I've cast the CustomizationContext object into a Template object. This
cast is working for me, so I'm guessing that this is a Template. Instead of
setting the value of Saved = true, I'm calling the Save() method after
deleting the items. This seems to work.
Are there any potential problems with saving normal.dot while Word is
shutting down?

Thanks.
David
 
D

David Thielen

There is a giant problem with this approach..

1) User makes some changes to normal.dot while editing.

2) On closing file, when asked if they want those changes saved, says NO.

3) You save them anyways.

I choose to not use this approach because this could leave the user with a
bunch of changes to normal (or whichever template you set) that the user does
not want.

- dave
 
K

Ken Slovak - [MVP - Outlook]

Don't call Save. Try CustomizationContext.Saved = True.

See Cindy's answer to another post here, she also recommends using
CustomizationContext.
 
D

David Thielen

Hi;

This is the solution I use but it's not 100%. In this case if the user then
makes a change of their own to normal.dot and is asked to save it, when they
do they also save your menu changes.

- dave
 
D

David

Ok, I see why it is better to set the Saved property to true instead of
saving the change on shutdown.

I have another idea to help get your solution closer to 100%. When loading,
in OnConnection() delete the menu item and then save the
CustomizationContext. Then add the button and set the
CustomizationContext.Saved property = true. This way, if the user does save
their normal.dot the button will be saved in it, but next time Word is
loaded, the button will be deleted from normal.dot. The button should only
remain if the user saves their normal.dot and then unloads the add-in.

Does that look good? Is it ok to save normal.dot in OnConnection?

Thank you both very much for your help.
David
 
D

David Thielen

Hi;

That sounds like a good idea - I'm going to try it. (Get us from 95% to 99%.)

thanks - dave
 
K

Ken Slovak - [MVP - Outlook]

What I usually do, at least when dealing with WordMail, is to immediately
set CustomizationContext.Saved = True after I add buttons. That's on the
document startup, so the assumption is that Normal.dot is clean at that
time.

If the user makes any changes to Normal.dot in the process of customizing
something CustomizationContext.Saved will be False unless they manually
saved Normal.dot. So when Document_Close fires (which would be before
Normal.dot is automatically saved or the user is prompted to save it) I
check for CustomizationContext.Saved = False. I then delete my buttons since
Word doesn't have the concept of temporary buttons as Outlook does.

If the saved value of CustomizationContext.Saved was False I then don't set
CustomizationContext.Saved = True and let the user get prompted to save or
the template gets saved normally. If it was True I assume no customizations
were made and I set it True again, since I just changed the state by
deleting the buttons.

That usually handles almost every case for me but your mileage may vary.
 

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