I'm working with InsertXML in C# for word 2007.

I've had several problems. The xml I'm inserting has word content controls in it. I am also regiserting for ContentControlAfterAdd. Using the word content controls I've set the tags as well. The insert seems to work fine for smaller amounts of text, but as I try to insert larger amounts, maybe 2 pages worth I run into a problem that sometimes one of the tags is null when the ContentControlAfterAdd Event handler is called. This is confusing because afterwords when I look at the tag of the word content control through the word document the tag is set appropritately.

I can only conclude that the ContentControlAfterAdd might be called before the tag value is set when adding word content controls via InsertXML! Is this known to be the case

To avoid this problem since it appears to be based on the size of the insert I thought I would break the insert up into sections.

This has led me to another problem: after calling InsertXML, the range object doesn't change immediately. I wanted to be able to call InsertXML repeadidly and have it append it to the end of the previous selection. A basic code snippet would be this:
Code Snippet
object direction = Word.WdCollapseDirection.wdCollapseEnd;

currentSelection.Range.Collapse(ref direction);
currentSelection.InsertXML(sb.ToString(), ref s);
currentSelection.Range.Collapse(ref direction);
currentSelection.InsertXML(sb2.ToString(), ref s);

1) Is the tag value be set in the xml and applied through InsertXML guarenteed to be set _before_ the event ContentControlAfterAdd is raised
2) How do I call InsertXML multiple times and have the cursor advance to the end of the previous call to InsertXML

Thanks for all the help in advance!


Re: Visual Studio Tools for Office InsertXML



2 things:

1) The constructor for the wcc does not have a tag in it, so It's more conceiveable that a race condition does exist here.

2) From the bizzare number department It always happens on the 93nd word content control in the InsertXML on my machine.


Re: Visual Studio Tools for Office InsertXML

Maarten van Stam

The question is not really VSTO related. It is helpful to remember that this forum is mainly for issues that directly pertain to the Visual Studio Tools for Office tools per se. So you will be best served by posing this question to a forum or newsgroup wholly dedicated to application-specific issues such as yours. Here is a link to resources that will help you:

It's not really an answer to you question, but could another solution to your scenario be to first collect all the XML strings and in the end insert the XML that you collected