How to change a node during an XMLSelectionChange event?

B

bwhalversen

Word 2003 sp2, VSTO 2005, C#

I have a need to change the text of the NewXmlNode passed as a paramter to
the application XMLSelectionChange event. My handler:

private void Application_XMLSelectionChange(
Word.Selection Sel,
Word.XMLNode OldXMLNode,
Word.XMLNode NewXMLNode,
ref int Reason)
{
if (NewXMLNode != null)
NewXMLNode.Range.Text = "Some Text"
}

The problem is that this causes a new SelectionChange event to fire that I
can make no sense out of. The new selection Word attempts to set appears to
be completely random. My understanding is that changing the Text propertry of
a Range should not affect the current Selection.

The result is that I get a new XMLSelectionChange event because Word thinks
it left the XMLNode it is currently in (the one whose text I just modified)
because of this "auto-generated" SelectionChange event.

The question may boil down to what can I safely do to the NewXMLNode passed
to the XMLSelectionChange event? Can I change the text of this node in this
event handler? If not, how and when can I make changes to the newly selected
node without causing Word to fire a new (and meaningless) SelectionChange
event?

Thanks for any help you can provide.
 
C

Cindy Meister

I believe this is an oddity of how XML nodes function in Word. The same thing
happens when text is changed in an Contentcontrol in Word 2007.

And I believe this is why there's also a REASON argument passed to the
event. Use that to determine what triggered the event so that you can
surpress the subsequent "loop".
 

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