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.
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.