How to protect bookmarks from accidental modification

C

Colin Chudyk

Hi,

I have a Word macro that performs a lot of operations on a document littered
with bookmarks. I want to allow a normal end-user to modify the formatting
and wording of the template without accidentally deleting a bookmark. I'm
not sure if this is even possible... is there no event to catch when a user
deletes a bookmark (for instance, by simply highlighting it with the cursor
and pressing the Delete key)?

I guess the next best thing would be to make it very apparent to the user
that the text they are editing is within a bookmark by highlighting it or
something. I could also check at the time of document closing whether all
the bookmarks still exist, and if not, abort the changes. But this is kind
of a bandaid solution.

Bottom line: how can I allow a typical end-user to modify the macro template
without chancing a meltdown or unpredictable results due to a bookmark
deletion or something similar?

Any ideas?

Thanks,
Colin
 
J

Jezebel

Can't be done. You can 'display' the bookmarks by Tools > Options > View >
Show Bookmarks: this puts square brackets around each bookmark.
Unfortunately these are ugly and annoying and they confuse the hell out of
most users.

But a bookmark is just a pair of pointers to locations in the document. If
the user is free to edit the document there's nothing you can do to prevent
them moving or deleting those locations. Nor are the events that Word raises
sufficient to tell you that it's happened.

You could make a list of the bookmarks when the document is opened, then
check that they're all still there in the DocumentBeforeSave event. Won't
tell you if they've moved inappropriately, though. You could also highlight
each bookmark at the start, then remove the highlighting at the end. But
ultimately it's a losing battle: a Word document is an inherently freeform
medium.
 
C

Colin Chudyk

Thanks Jezebel,

That's exactly what I was thinking. I guess I'll have to do some
highlighting to let the user know what they're modifying, and check the
bookmarks at the open and close events. I'll probably implement a routine to
keep backups of the document so that if they mess something up it can easily
be reverted to its prior Last Known Good state.

Colin
 
J

JGM

Hi Colin,

Just thought of something....

Since your biggest concern is that the bookmarks might be deleted, you can
catch the Delete and Cut events by writing macros named EditCut and
EditClear (you might want to check the exact names in the Customize,
Keyboard dialog box),. These macro could, for example, check if the current
selection contains a bookmark, if not go ahead, if so, warn user and exit or
something like that... Like I said, it is just a thought! I have not tested
it or anything like that, but I do know that you can intercept these events,
so I guess you could do something with that...

HTH
Cheers!
 
J

Jezebel

Not enough. Select a range of text that spans one or more bookmarks and type
anything. You've just deleted the bookmarks, with no event fired to tell
you. Even using the WindowSelectionChange event to detect if the selection
includes a bookmark won't work: select a point outside a bookmark then use
the arrow keys to extend the selection over a bookmark -- that fires no
event either.
 
J

Jezebel

If it's critical, and you've got the time to deal with it, take an entirely
different approach. Put the critical information into
CustomDocumentProperties and display it in the document using DocProperty
fields.

You'd have to do one pass through the documents to 'fix' them -- read the
bookmarks, save the content as properties, replace the bookmark with a
field. After that, users would have to know to use the Properties dialog (or
your own UserForm) to edit the values; but the values would be protected
from clumsy users.
 

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