PDA

View Full Version : Solved: Menu Bar Changes On Demand



xCav8r
02-24-2006, 02:58 PM
I couldn't think of a more descriptive title. http://vbaexpress.com/forum/images/smilies/119.gif

I have a group of approximately 100 people who are converting from Word Perfect to Word. They will be using MS Word templates created by a small group of people with no knowledge of (or desire to know) VBA. Many of these templates will use Word's formfield controls (user's choice), and the templates will be saved with protection without a password. I have been asked to provide a few macros that the templates can use?namely inserting the current date to a specially named formfield on document_new, spell checking protected forms, and unlinking form fields. Other things may be added in the future, so I chose to place the macros in a few modules in a template called "templatemacros.dot", which is stored in a read-only directory on a server to which the 100+ users are pointing via their startup paths. The idea is to make future distribution of the code as simple as possible and not burden the template creators with the need to adjust their security settings or think about storing these templates in trusted locations.

I instructed the template creators to add two custom document properties to their templates to enable the use of the macros. The first property is called "ShowMenu". I've been trapping DocumentChange to look for the custom property and, if found, display a pop-up tacked onto the end of the Menu Bar. The pop-up, which is stored in the template containing these macros, has two buttons pointing to the routines that unlink the fields and spell check protected forms, respectively. The second property is called "InsertDate". I'm trapping the document_new to look for this property and, when found, to insert the date to the desired field.

Everything works as planned except for one frustrating thing. I'm displaying the pop-up by turning its visibility on and off, but whenever a user quits Word, he or she is prompted to save changes to "templatemacros.dot". Changing the visibility in the pop-up, which is stored in "templatemacros.dot" is causing the prompt to appear. That won't work with a template that's in a read-only directory?not to mention that the file is locked whenever anyone pointing to it via their startup path has Word open.

As a workaround, I've considered creating and deleting the pop-up and corresponding buttons in each user's "normal.dot". I always have bad luck with my "normal.dot" getting corrupted in some way?especially following modifications to commandbars?so I'm concerned that constantly editing each user's "normal.dot" might accelerate the demise of their files. Perhaps this is an unwarranted concern.

I hope that provides enough background to ask these questions:

Given what I'm trying to do, is there a better way to accomplish the end result? (Sometimes I can't see the forest.)
If storing the code, pop-up, and corresponding buttons in a global template is as good as any other approach, is there a way to prevent the prompt to save without resorting to changes to the "normal.dot"?
Am I unnecessarily concerned with the "normal.dot" becoming corrupted?

TonyJollans
02-24-2006, 04:07 PM
Hi Marco,

I haven't read all of your post properly but try doing something like this ...SavedState = Templates("templatemacros.dot").Saved
yourpopup.visible = true
Templates("templatemacros.dot").Saved = SavedStateThe visibility changes should not then trigger the "do you want to save" message.

TonyJollans
02-24-2006, 04:21 PM
Actually, having read a bit more - if the template is read-only for all users and they can never update it for any reason, you might just want to use Templates("templatemacros.dot").Saved = True at appropriate places in your code.

xCav8r
02-24-2006, 04:23 PM
:omg2:

I'm always overlooking something simple. I didn't realize there was a RW property of documents and templates for saved state. Sweet! You rock, TJ!!!

TonyJollans
02-24-2006, 04:32 PM
Perhaps I should read this properly before posting again ...

I wonder if there isn't a better way to create your menu item - but, doing it as you are, wouldn't DocumentOpen be a better event to trap? And maybe DocumentBeforeClose to remove the item. But if you have something working ....


Normal.dot corruption -- Firstly, don't mess with user's normal.dots. Secondly my personal opinion is that corruption is often blamed when the real problem is just that somebody doesn't know what settings have been changed or how to change them back. Just because deleting normal.dot fixes a problem does not mean it was corrupt. Yes, it can happen, and adding and removing toolbars does seem to be problematic on occasion.

xCav8r
02-24-2006, 04:52 PM
I wonder if there isn't a better way to create your menu item - but, doing it as you are, wouldn't DocumentOpen be a better event to trap? And maybe DocumentBeforeClose to remove the item. But if you have something working ....

I chose DocumentChange because I only want the pop-up to appear on the MenuBar when users are looking at documents with "ShowMenu" as a custom property. If I toggled the visibility at DocumentOpen and back off at DocumentBeforeClose, then the pop-up would be visible for all documents (even those without the custom property) until a document containing the custom property is closed, and if a user still has another document open with the same custom property, then the pop-up would be hidden when it should be displayed. Does that make sense, or am I missing something (which is often the case)? :think:


Normal.dot corruption -- Firstly, don't mess with user's normal.dots. Secondly my personal opinion is that corruption is often blamed when the real problem is just that somebody doesn't know what settings have been changed or how to change them back. Just because deleting normal.dot fixes a problem does not mean it was corrupt. Yes, it can happen, and adding and removing toolbars does seem to be problematic on occasion.

Thanks for the feedback. :friends:

TonyJollans
02-24-2006, 05:02 PM
If you set the CustomizationContext to the Template which needs it instead of letting it default to the NormalTemplate then the control will only show on the menu bar in documents using that template.

xCav8r
02-24-2006, 05:15 PM
If you set the CustomizationContext to the Template which needs it instead of letting it default to the NormalTemplate then the control will only show on the menu bar in documents using that template.

I think I'm still missing something. :dunno

The CustomizationContext points to "templatemacros.dot", which is where the pop-up, corresponding buttons, and shared code reside. (I only considered using "normal.dot" as a workaround to the prompt.) The templates that are being made by the aforementioned group of template creators do not contain the pop-up or corresponding buttons, nor do they have any code.

TonyJollans
02-24-2006, 05:34 PM
If I understand correctly, you have lots of individual templates each with indicators for you to check - and one global template - plus normal of course.

Toolbar changes in normal and your global templatesmacros will affect every window.
Toolbar changes in individual templates will only affect windows where those templates play a part.

So instead of using Customization.Context = Templates("templatesmacros.dot")use Customization.Context = ActiveDocument.AttachedTemplate

TonyJollans
02-24-2006, 05:37 PM
Oh, and, of course, wrap it upSavedState = ActiveDocument.AttachedTemplate.Saved
Customization.Context = ActiveDocument.AttachedTemplate
' Make the Toolbar changes
ActiveDocument.AttachedTemplate.Saved = SavedState

xCav8r
02-24-2006, 06:01 PM
If I understand correctly, you have lots of individual templates each with indicators for you to check - and one global template - plus normal of course.

Yes, you correctly understood what I wrote, but not thinking that the discussion would get this involved, I had avoided some details now relevant. There are two distinct smaller groups of people in the 100+ that will have three templates loaded globally (from their startup path)--one of which is the "templatemacros.dot". There are also other workgroups outside this 100+ that are already loading other global templates, and they will be in a similar situation after they adopt what I work out for this current 100+ (since they too will have a need to convert their WP templates to MS Word). Anyway, this is why I have avoided using the AttachedTemplate property, because I assumed that I would eventually get errors if I didn't refer to them explicitly.


Toolbar changes in normal and your global templatesmacros will affect every window.

That's why I chose DocumentChange, because... (see below)


Toolbar changes in individual templates will only affect windows where those templates play a part.

...the pop-up and corresponding buttons only exist in "templatemacros.dot", so I don't understand what implicitly or explicitly setting the CustomizationContext to, say, "templateA.dot"--a template containing no custom commandbars--will produce besides err.Number=5. I still don't understand why DocumentChange isn't the right event.

:hairpull:

TonyJollans
02-25-2006, 02:40 AM
Ah, OK. So the toolbar customizations are in the global template - you don't create them every time - you just change their visibility on document change. Sorry, I misunderstood, but I've had a sleep now!