Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / August 2006

Tip: Looking for answers? Try searching our database.

cancel XMLBeforeDelete event

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Helen Hu - 14 Aug 2006 19:28 GMT
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
Cindy M. - 15 Aug 2006 10:21 GMT
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 :-)
Sylvain - 17 Aug 2006 20:18 GMT
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.

> Hi Helen,
>
[quoted text clipped - 13 lines]
> This reply is posted in the Newsgroup; please post any follow question or reply
> in the newsgroup and not by e-mail :-)
Helen Hu - 18 Aug 2006 01:17 GMT
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

> Hi Helen,
>
[quoted text clipped - 13 lines]
> This reply is posted in the Newsgroup; please post any follow question or reply
> in the newsgroup and not by e-mail :-)
Cindy M. - 18 Aug 2006 10:37 GMT
Hi Helen / Sylvain,

> It seems both Sylvain and I are facing the same issue.
>  
[quoted text clipped - 9 lines]
>  
> 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 :-)
Sylvain - 18 Aug 2006 19:15 GMT
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.

> Hi Helen / Sylvain,
>
[quoted text clipped - 55 lines]
> This reply is posted in the Newsgroup; please post any follow question or
> reply in the newsgroup and not by e-mail :-)
Cindy M. - 19 Aug 2006 15:33 GMT
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 :-)
Helen Hu - 18 Aug 2006 21:39 GMT
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

> Hi Helen / Sylvain,
>
[quoted text clipped - 55 lines]
> This reply is posted in the Newsgroup; please post any follow question or
> reply in the newsgroup and not by e-mail :-)
Cindy M. - 19 Aug 2006 15:33 GMT
Hi Helen,

> Thanks a lot for your help.
>  
[quoted text clipped - 11 lines]
> 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 :-)
Sylvain - 20 Aug 2006 21:01 GMT
Thanks Cindy for all your help.

> Hi Helen,
>
[quoted text clipped - 26 lines]
> This reply is posted in the Newsgroup; please post any follow question or
> reply in the newsgroup and not by e-mail :-)
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.