VSTO XmlNode Text

C

Chris

I have started working with VSTO to create a Word 2007 Add-in for the purpose
of adding custom XML markup to documents to aid in our business workflows. As
many before me have mentioned the Documentation leaves much to be desired. I
have been able to accomplish what I was hoping though I have run into a bit
of a quirk that I can’t seem to figure out. The purpose of the plug-in is to
allow our users to bring content from our proprietary software application
into a word document. Within the document they may edit the content and then
update the data store with their changes. My approach was to use VSTO to
insert a custom XML node into the word document and then set the XML node’s
text. The custom XML node contains a number of attributes which are used by
the plug-in to perform content synchronization with the proprietary data
store.

The problem I have run into seems to be due to new line characters (“\r\nâ€)
in the text content. When inserting a single xml node in the fashion
described above there is no problem. The XML Node contains one or more
Word.Paragraphs representing the text. However, when a number of XML nodes
are added to the active selection in succession (see the code below), the
text containing newline characters leave the confines of the custom XML Node
tags, resulting in only a portion of the text appearing within the intended
XML Node. The remainder appears outside the XML Node directly after. From
what I have seen the only the text up to the first newline character is
placed within the tags.

Here is the simplified code to give you an idea how I am doing things.

Word.Document nativeDocument = this.Application.ActiveDocument;
Word.Range range = Application.Selection.Range;
object oRange = range;
Word.XMLNode node = null;
foreach (string text in textCollection)
{
oRange = range;
node = nativeDocument.XMLNodes.Add("mec", "mec", ref oRange);
Word.XMLNode attr = node.Attributes.Add("data", "some value", ref missing);
node.Range.Text = text;
range.SetRange(node.Range.End + 1, node.Range.End + 1);
}

I am against stripping out the newline characters as they are essential to
the layout of the content. I am guessing my problem is my use of the Range
object, which unfortunately the documentation doesn’t provide a great deal of
insight beyond the bare minimum.

Thankyou in advance.
 
C

Cindy M.

Hi Chris,

First, I need to ask why you're using XMLNodes rather than ContentControls,
linking the ContentControls to a CustomXMLPart in the Word 2007 docx/docm
document? This would be much more efficient, wouldn't entail attaching a schema,
allow you to protect the controls from deletion, and perhaps take care of the
problem you're seeing.

OTOH it might not change things, depending on how you're expecting it to work.
Word *canNOT* do this with XMLNodes (or ContentControls):

<node1>Here's some text. <node2>More text, and now I press ENTER
So here in the next paragraph.</node2></node1>

A single node can contain multiple paragraphs, as well as other nodes.

No node can start in the middle of a pargraph, then break to another paragraph. It
has to do with how Word is structured, internally.

Word bookmarks can do this, but they can't hold any "metadata". You could insert a
PRIVATE field at the front of a bookmark to carry such information. Or create
document Variables (an object in the Word object model), named the same as a
bookmark, to hold the information.
I have started working with VSTO to create a Word 2007 Add-in for the purpose
of adding custom XML markup to documents to aid in our business workflows. As
many before me have mentioned the Documentation leaves much to be desired. I
have been able to accomplish what I was hoping though I have run into a bit
of a quirk that I can’t seem to figure out. The purpose of the plug-in is to
allow our users to bring content from our proprietary software application
into a word document. Within the document they may edit the content and then
update the data store with their changes. My approach was to use VSTO to
insert a custom XML node into the word document and then set the XML node’s
text. The custom XML node contains a number of attributes which are used by
the plug-in to perform content synchronization with the proprietary data
store.

The problem I have run into seems to be due to new line characters (“\r\nâ€)
in the text content. When inserting a single xml node in the fashion
described above there is no problem. The XML Node contains one or more
Word.Paragraphs representing the text. However, when a number of XML nodes
are added to the active selection in succession (see the code below), the
text containing newline characters leave the confines of the custom XML Node
tags, resulting in only a portion of the text appearing within the intended
XML Node. The remainder appears outside the XML Node directly after. From
what I have seen the only the text up to the first newline character is
placed within the tags.

Here is the simplified code to give you an idea how I am doing things.

Word.Document nativeDocument = this.Application.ActiveDocument;
Word.Range range = Application.Selection.Range;
object oRange = range;
Word.XMLNode node = null;
foreach (string text in textCollection)
{
oRange = range;
node = nativeDocument.XMLNodes.Add("mec", "mec", ref oRange);
Word.XMLNode attr = node.Attributes.Add("data", "some value", ref missing);
node.Range.Text = text;
range.SetRange(node.Range.End + 1, node.Range.End + 1);
}

I am against stripping out the newline characters as they are essential to
the layout of the content. I am guessing my problem is my use of the Range
object, which unfortunately the documentation doesn’t provide a great deal of
insight beyond the bare minimum.

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

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