cancel XMLBeforeDelete event

H

Helen Hu

I am trying to handle XMLBeforeDelete event. I want to check the name of the
node and disallow the delete of this node. How can I cancel this delete from
my program?

Thanks a lot,
Helen
 
C

Cindy M.

Hi Helen,
I am trying to handle XMLBeforeDelete event. I want to check the name of the
node and disallow the delete of this node. How can I cancel this delete from
my program?
Word 2003, I assume. In which environment (Word VBA, a COM Addin, VSTO, .NET
automation) is your program running? What code do you currently have (that, I
take it, is not working)?

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 :)
 
S

Sylvain

Hi, I am doing the exact same thing, I am using Word 2003, VB.NET, and
a smart tag solution integrated into the task pane of a word document.

When the user deletes a node, I can catch the XMLBeforeDelete event,
that's great, but how do I actually stop the delete action?? is that
possible??

Thanks a lot, this would solve a major problem that we are having.

sylvain.
 
H

Helen Hu

Hi Cindy,

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?

Thanks,
Helen
 
C

Cindy M.

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 :)
 
S

Sylvain

Thank you the quick reply, that's what I ended up doing. I can't use
the protection because I want them to allow the deletion of a tag only
if it isn't referenced by another tag in another document.

The only problem that I'm having now, is that the XMLBeforeDelete event
will be thrown if a tag is cut and pasted or even if it is being
moved. Is there a way of knowing if that is being done at the present
time, a bit like the undo-redo.

Thanks,

sylvain.
 
H

Helen Hu

Thanks a lot for your help.

I added the code (in c#):
Word.Range rng = range.Duplicate;
object collapse = Word.WdCollapseDirection.wdCollapseStart;
rng.Collapse(ref collapse);
object r = range;
Word.XMLNode newNode =
range.XMLParentNode.ChildNodes.Add(node.BaseName,node.NamespaceURI,ref r);

Somehow the newly added node only has the starting tag. The closing code is
not added.

Another problem is that when there are two nodes in the selection, there
will be a problem.

Thanks again!
Helen
 
C

Cindy M.

Hi Helen,
Thanks a lot for your help.

I added the code (in c#):
Word.Range rng = range.Duplicate;
object collapse = Word.WdCollapseDirection.wdCollapseStart;
rng.Collapse(ref collapse);
object r = range;
Word.XMLNode newNode =
range.XMLParentNode.ChildNodes.Add(node.BaseName,node.NamespaceURI,ref r);

Somehow the newly added node only has the starting tag. The closing code is
not added.

Another problem is that when there are two nodes in the selection, there
will be a problem.
Yes, I mentioned this was just a simple example of how to approach it. You'll
need to work on creating a solution to fit your specific needs. The first
problem may have to do with the type of tag you were testing on. You may need
to pick up all the child, or even parent elements and recreate those, as well.

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 :)
 
C

Cindy M.

Hi Sylvain,
The only problem that I'm having now, is that the XMLBeforeDelete event
will be thrown if a tag is cut and pasted or even if it is being
moved. Is there a way of knowing if that is being done at the present
time, a bit like the undo-redo.
That's more difficult, especially since you're not working in a Word
VBA project that will let you intercept commands. You may well need to
pair this event with XMLAfterInsert. Perhaps saving the objects created
in XMLBeforeDelete and if/when XMLAfterInsert fires, compare what's being
inserted with the saved objects. If they match, you know you'll need to
remove the objects you created?

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 :)
 
S

Sylvain

Thanks Cindy for all your help.
Hi Helen,

Yes, I mentioned this was just a simple example of how to approach it. You'll
need to work on creating a solution to fit your specific needs. The first
problem may have to do with the type of tag you were testing on. You may need
to pick up all the child, or even parent elements and recreate those, as well.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)


This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

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