enabling/disabling menu items on the fly

C

Chip Orange

I have a template with a custom menu added to it. I'd like to enable and
disable some of the menu items on the fly as the user makes various choices
(as well as when opening or creating a document).

The problem is that this evidently causes Word to think that the template
has been modified, so the users are prompted to save the template when they
exit.

Is there anything that I can do to keep Word from prompting the users about
changes to the template (obviously, I'm not talking about normal.dot here).

thanks.

Chip
 
J

Jonathan West

Hi Chip

Assuming that the add-in contains both the code and the menus, do the
following

Immediately before each change to the menu, add the following line of code

Application.CustomizationContext = ThisDocument

Immediately after each change to the menu, add the following line of code.

ThisDocument.Saved = True

This will ensure that the changes to the menus are added to your add-in and
not anywhere else, and also that they are discarded when the add-in is
unloaded.

You might also want to add the following macro as a belt-and-braces
safeguard

Sub AutoExit()
ThisDocument.Saved = True
End Sub
 
C

Chip Orange

Thanks so much for your prompt reply Jonathan.


I had been trying to use the CustomizationContext property, but it wasn't
having any effect (that is, I would set it to the activedocument object
before making any changes to the CommandBars, and yet still receive the
prompt).

I'm not familiar with the ThisDocument object, does it represent the
template or the document being created from the template? If it's the
latter, I wouldn't want to keep indicating that it was saved or the user
would not be prompted when they should to save their work right?

What I have done, and which seems to be working for me, is to add the line:

ActiveDocument.AttachedTemplate.Saved = True

after each modification.

Is this effectively the same as your recommendation, if not, do you see any
problems with it?

thanks.

Chip
 
J

Jonathan West

Chip Orange said:
Thanks so much for your prompt reply Jonathan.


I had been trying to use the CustomizationContext property, but it wasn't
having any effect (that is, I would set it to the activedocument object
before making any changes to the CommandBars, and yet still receive the
prompt).

I'm not familiar with the ThisDocument object, does it represent the
template or the document being created from the template? If it's the
latter, I wouldn't want to keep indicating that it was saved or the user
would not be prompted when they should to save their work right?

It represents the file containing the code that is currently running. That
can be a document, a template or an add-in, according to circumstance.

What I have done, and which seems to be working for me, is to add the
line:

ActiveDocument.AttachedTemplate.Saved = True

after each modification.

Is this effectively the same as your recommendation, if not, do you see
any
problems with it?

It's not necessarily the same. There are two problems with that approach.

1. You don't mention that you have set the CustomizationContext. That means
you aren't controlling where changes are getting written to. If you aren't
sure you undesratnd the CustomizationContext property, think of the Tools
Customize dialog, and the dropdown listbox that defines where customizations
are saved. That is the CustomizationContext. You have no way of knowing that
the user hasn't manually set the customization context in Tools Customize,
so you must set it yourself in code each time you make the change.

2. Using ThisDocument instead of ActiveDocument.AttachedTemplate means that
it is easier to re-use your code if you decide to do something similar as an
add-in in future.
 

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