Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Intro

Excerpt

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.

Code

Code Block
  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(@"..\..\TemplatesWordTemplateFiles\MergeDocuments1mergedocuments1.doc");

 // Append first document 
 doc.Append(doc1);

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

 // Append second document  
 doc.Append(doc2);

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

 // Append third document 
 doc.Append(doc3);

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

Download

Files to be Merged:

...

...

Output File: MergedDocument_out.doc