Hi Helen / Sylvain,
It seems both Sylvain and I are facing the same issue.
I am using Word 2003, .net Word object model. In my beforeDeleteHandler:
public void beforeDeleteHandler(Range range, XMLNode node, bool InUndoRedo)
{
if (!InUndoRedo) {
if (node.BaseName.Equals("SomeImportantNode") {
//cancel the event
}
}
}
How can we cancel this delete event?
As you can see from the event signature, it doesn't offer you a way to cancel
the event. That means you have to store the information and recreate it.
Here's an example that assumes a single node is being deleted. It recreates
the node just in front of the one that's being deleted.
Dim rng As Word.Range
Dim newXMLNode As XMLNode
Set rng = DeletedRange.Duplicate
rng.Collapse wdCollapseStart
Set newXMLNode = ThisDocument.XMLNodes.Add(OldXMLNode.BaseName,
OldXMLNode.NamespaceURI, DeletedRange)
newXMLNode.Text = OldXMLNode.Text
translated, off the top of my head, into C#
Word.Range rng = DeletedRange.Duplicate;
rng.Collapse(Word.WdCollapseDirection.wdCollapseStart);
Word.XMLNode newXMLNode =
DeletedRange.Parent.XMLNodes.Add(OldXMLNode.BaseName, OldXMLNode.NamespaceURI,
DeletedRange);
newXMLNode.Text = OldXMLNode.Text;
As an alternative, you could consider applying "Read-only" protection to the
XML tags. This will disallow certain commands (mostly, working with "floating"
graphics - but there are ways around that if your users really need to insert
graphics with text flow), but will prevent anyone from deleting the XML tags.
Basically, all you need to do is
- Tools/Protect Document
- Select "Allow only this type of editing" + No changes (Read only)
- select the text between the tags that the user is allowed to change, then
click "Everyone"
- Start enforcing protection
Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org
This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail
