0

I have multiple <body> tags from an word document. I do this with the open xml sdk. So the new document should generated with openxml

The body's comes from

WordprocessingDocument.Open("C:\Temp\Test.docx").MainDocumentPart.Document.Body.OuterXml

I have so different body's in a list. With al different values. Changed some text in the xml. And saved them in a new list.

Now must that list in an new word document. How can i do that? I tried altChunk. But my word document is always corrupt.

Somebody that can help me?

jjnguy
  • 136,852
  • 53
  • 295
  • 323
Sven
  • 885
  • 5
  • 11
  • 31

2 Answers2

0

This function will throw exception if you are adding yourbodylist from another document without cloning. We have to use CloneNode(deep:true) for each body elements.

WordProcessingDocument.Create("path_and_name_with_.docx").MainDocumentPart.Document.append(yourBodyList); This might cause exception. Refer this post: Cannot insert the OpenXmlElement "newChild" because it is part of a tree

CloneNode(true) will create clone of the element and without any links or references to the parent. And for your multi Body problem. get the child elements of each body and add it to a new Body() element. Hope this helps!

Community
  • 1
  • 1
Mohamed Alikhan
  • 1,315
  • 11
  • 14
0

You can create a WordDocument using the sdk. I think it's

WordProcessingDocument.Create("path_and_name_with_.docx").MainDocumentPart.Document.append(yourBodyList);

You could also take the resulting documents that don't work and look at it's XML using the SDK tool or just rename the doc to .zip extension and see why it doens't work.

Ingó Vals
  • 4,788
  • 14
  • 65
  • 113
  • It works like this. But i went all the body's in 1 word document. But i solved it by creating more documents – Sven Sep 16 '10 at 21:04
  • I do believe you can only have one Body in a document. But you could remove the paragraphs from within the other bodies and add them all to the new documents body. – Ingó Vals Feb 20 '11 at 01:14