title: Merge multiple word documents into one Open Xml
body: <p>I have around 10 word documents which I generate using open xml and other stuff.
  Now I would like to create another word document and one by one I would like to join them into  this newly created document.
  I wish to use open xml, any hint would be appreciable.
  Below is my code:</p>
  
  <pre><code> private void CreateSampleWordDocument()
      {
          //string sourceFile = Path.Combine("D:\\GeneralLetter.dot");
          //string destinationFile = Path.Combine("D:\\New.doc");
          string sourceFile = Path.Combine("D:\\GeneralWelcomeLetter.docx");
          string destinationFile = Path.Combine("D:\\New.docx");
          try
          {
              // Create a copy of the template file and open the copy
              //File.Copy(sourceFile, destinationFile, true);
              using (WordprocessingDocument document = WordprocessingDocument.Open(destinationFile, true))
              {
                  // Change the document type to Document
                  document.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
                  //Get the Main Part of the document
                  MainDocumentPart mainPart = document.MainDocumentPart;
                  mainPart.Document.Save();
              }
          }
          catch
          {
          }
      }
  </code></pre>
  
  <p><strong>Update( using AltChunks):</strong></p>
  
  <pre><code>using (WordprocessingDocument myDoc = WordprocessingDocument.Open("D:\\Test.docx", true))
          {
              string altChunkId = "AltChunkId" + DateTime.Now.Ticks.ToString().Substring(0, 2) ;
              MainDocumentPart mainPart = myDoc.MainDocumentPart;
              AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(
                  AlternativeFormatImportPartType.WordprocessingML, altChunkId);
              using (FileStream fileStream = File.Open("D:\\Test1.docx", FileMode.Open))
                  chunk.FeedData(fileStream);
              AltChunk altChunk = new AltChunk();
              altChunk.Id = altChunkId;
              mainPart.Document
                  .Body
                  .InsertAfter(altChunk, mainPart.Document.Body.Elements&lt;Paragraph&gt;().Last());
              mainPart.Document.Save();
          } 
  </code></pre>
  
  <p>Why this code overwrites the content of the last file when I use multiple files?
  <strong>Update 2:</strong></p>
  
  <pre><code> using (WordprocessingDocument myDoc = WordprocessingDocument.Open("D:\\Test.docx", true))
          {
  
              MainDocumentPart mainPart = myDoc.MainDocumentPart;
              string altChunkId = "AltChunkId" + DateTime.Now.Ticks.ToString().Substring(0, 3);
              AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML, altChunkId);
              using (FileStream fileStream = File.Open("d:\\Test1.docx", FileMode.Open))
              {
                  chunk.FeedData(fileStream);
                  AltChunk altChunk = new AltChunk();
                  altChunk.Id = altChunkId;
                  mainPart.Document
                      .Body
                      .InsertAfter(altChunk, mainPart.Document.Body
                      .Elements&lt;Paragraph&gt;().Last());
                  mainPart.Document.Save();
              }
              using (FileStream fileStream = File.Open("d:\\Test2.docx", FileMode.Open))
              {
                  chunk.FeedData(fileStream);
                  AltChunk altChunk = new AltChunk();
                  altChunk.Id = altChunkId;
                  mainPart.Document
                      .Body
                      .InsertAfter(altChunk, mainPart.Document.Body
                      .Elements&lt;Paragraph&gt;().Last());
              }
              using (FileStream fileStream = File.Open("d:\\Test3.docx", FileMode.Open))
              {
                  chunk.FeedData(fileStream);
                  AltChunk altChunk = new AltChunk();
                  altChunk.Id = altChunkId;
                  mainPart.Document
                      .Body
                      .InsertAfter(altChunk, mainPart.Document.Body
                      .Elements&lt;Paragraph&gt;().Last());
              } 
          }
  </code></pre>
  
  <p>This code is appending the Test2 data twice, in place of Test1 data as well.
  Means I get:</p>
  
  <pre><code>Test
  Test2
  Test2
  </code></pre>
  
  <p>instead of :</p>
  
  <pre><code>Test
  Test1
  Test2
  </code></pre>