If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. |
|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
Inserting block level xml fragments.
I am trying to insert the following xml fragment into a WordML document programmatically:
ns0:catalog .... ns0:item ns0:title wlaceholder="Enter item title." w wPr wStyle w:val="Heading2" / /wPr /w /ns0:title ns0:description wlaceholder="Enter item description." w wPr wStyle w:val="Normal" / /wPr /w /ns0:description ns0:item .... /ns0:catalog But, i cannot achieve the result no matter what I try. I am getting errors like Xml node cannot be inserted at current selection, null object reference exceptions, etc. Title item gets created and than it appears deleted when InsertParagraphAfter() is called(can see it when the revision tracking is on, plus XmlBeforeDelete event fires), which violates the document schema. Plus, another paragraph gets created between the item and the title nodes. I am sick and tired of fighting with the paragraphs already. Did anyone else run into this kind of issues? Does anyone have a solution or knows how to overcome this? here is the code that I use: /// summary /// Adds new item xml node /// /summary internal Word.XMLNode AddItem(Word.XMLNode parentNode, string titlePlaceholderText, string descriptionPlaceholderText) { Word.XMLNode itemNode = null; object start = parentNode.Range.End - 1; object end = parentNode.Range.End - 1; object rng = this.ThisDocument.Range(ref start, ref end); itemNode = parentNode.XMLNodes.Add("title", this.XmlNamespace, ref rng); if (itemNode==null) { throw new ExecutionEngineException("Unable to create item node"); } itemNode.Range.InsertParagraph(); //TODO: Set paragraph style rng = itemNode.Range; Word.XMLNode titleNode = itemNode.XMLNodes.Add("title", this.XmlNamespace, ref rng); if (titleNode==null) { throw new ExecutionEngineException("Unable to create title node"); } titleNode.PlaceholderText = "Enter title here"; titleNode.Range.InsertParagraphAfter(); //TODO: Set paragraph style start = titleNode.Range.End; end = itemNode.Range.End; rng = this.ThisDocument.Range(ref start, ref end); Word.XMLNode descriptionNode = itemNode.XMLNodes.Add("description", this.XmlNamespace, ref rng); if (descriptionNode==null) { throw new ExecutionEngineException("Unable to create description node"); } descriptionNode.PlaceholderText = "Enter title here"; titleNode.Range.Select(); return itemNode; } Sincerely, Ruslan (nospam)newsgroups(nospam)(at)(nospam)ruslan(nospa m)(dot)ca |
#2
|
|||
|
|||
Inserting block level xml fragments.
If you don't get the answer you are looking for here you might try one of the developer newsgroups since that's where the VBA and XML gurus tend to hang out. :-)
-- Please post all follow-up questions to the newsgroup. Requests for assistance by email can not be acknowledged. ~~~~~~~~~~~~~~~ Beth Melton Microsoft Office MVP Word FAQ: http://mvps.org/word TechTrax eZine: http://mousetrax.com/techtrax/ MVP FAQ site: http://mvps.org/ "anonymous" wrote in message news:xjpyc.1184$0A.9016@localhost... I am trying to insert the following xml fragment into a WordML document programmatically: ns0:catalog ... ns0:item ns0:title wlaceholder="Enter item title." w wPr wStyle w:val="Heading2" / /wPr /w /ns0:title ns0:description wlaceholder="Enter item description." w wPr wStyle w:val="Normal" / /wPr /w /ns0:description ns0:item ... /ns0:catalog But, i cannot achieve the result no matter what I try. I am getting errors like Xml node cannot be inserted at current selection, null object reference exceptions, etc. Title item gets created and than it appears deleted when InsertParagraphAfter() is called(can see it when the revision tracking is on, plus XmlBeforeDelete event fires), which violates the document schema. Plus, another paragraph gets created between the item and the title nodes. I am sick and tired of fighting with the paragraphs already. Did anyone else run into this kind of issues? Does anyone have a solution or knows how to overcome this? here is the code that I use: /// summary /// Adds new item xml node /// /summary internal Word.XMLNode AddItem(Word.XMLNode parentNode, string titlePlaceholderText, string descriptionPlaceholderText) { Word.XMLNode itemNode = null; object start = parentNode.Range.End - 1; object end = parentNode.Range.End - 1; object rng = this.ThisDocument.Range(ref start, ref end); itemNode = parentNode.XMLNodes.Add("title", this.XmlNamespace, ref rng); if (itemNode==null) { throw new ExecutionEngineException("Unable to create item node"); } itemNode.Range.InsertParagraph(); //TODO: Set paragraph style rng = itemNode.Range; Word.XMLNode titleNode = itemNode.XMLNodes.Add("title", this.XmlNamespace, ref rng); if (titleNode==null) { throw new ExecutionEngineException("Unable to create title node"); } titleNode.PlaceholderText = "Enter title here"; titleNode.Range.InsertParagraphAfter(); //TODO: Set paragraph style start = titleNode.Range.End; end = itemNode.Range.End; rng = this.ThisDocument.Range(ref start, ref end); Word.XMLNode descriptionNode = itemNode.XMLNodes.Add("description", this.XmlNamespace, ref rng); if (descriptionNode==null) { throw new ExecutionEngineException("Unable to create description node"); } descriptionNode.PlaceholderText = "Enter title here"; titleNode.Range.Select(); return itemNode; } Sincerely, Ruslan (nospam)newsgroups(nospam)(at)(nospam)ruslan(nospa m)(dot)ca |
Thread Tools | |
Display Modes | |
|
|