WordWriter allows you to insert elements from separate documents. You can use this functionality to copy/paste document elements into different files, or to easily merge Word documents. This sample demonstrates how to merge three different Word documents into one unified document.

This sample uses the WordApplication class to open three existing documents and insert them into a new blank document. The InsertBefore() and InsertAfter() methods are used, which accept an element from the current document or any other document, inserting it into the current document.


 public void GenerateDocument()
 // Initialize the WordApplication and create a blank document. 
 WordApplication app = new WordApplication();

 Document doc = app.Create();

 // Open first document to merge 
 Document doc1 = app.Open(@"..\..\WordTemplateFiles\mergedocuments1.doc");

 // Append first document 

 // Open second document to merge 
 Document doc2 = app.Open(@"..\..\WordTemplateFiles\mergedocuments2.doc");

 // Append second document  

 // Open third document to merge 
 Document doc3 = app.Open(@"..\..\WordTemplateFiles\mergedocuments3.doc");

 // Append third document 

 // Save the merged document 
 app.Save(doc, @"..\..\WordOutputFiles\MergedDocument_out.doc");


Files to be Merged:

Output File: MergedDocument_out.doc