How to Merge Two Word Documents into One by Using C#/VB.NET

Sometimes, people would like to merge two or more Word documents into one because these documents are talking about one topic or each of them is one part of article or story. Generally speaking, contents in documents will not be influenced after merging. It means that the contents will be the same as the original documents.  But before merging, we should pay attention to merging order.

In this post, I will share an easy method to merge Word with C#/VB.NET.

In my example, I prepare two documents. One is overview of New Zealand and the other is New Zealand history. I need to merge the two into one and the contents in first documents will be put on front of history. 

Note: a .NET Word componentSpire.Doc for .NET is used in this method, so I have added its dll file as reference in my project at the beginning.

Steps:

  1. Load document Introduction and History from computer.
  2. Merge two documents by using for each sentence. Get sections in document History and then add these sections to document Introduction.
  3. Save and launch file.

Main Coding:

C#:

using System;
using Spire.Doc;
using Spire.Doc.Documents;

namespace Sample
{
    class Merge
    {
        static void Main(string[] args)
        {
            //Load Document1 and Document2
            Document document = new Document();
            document.LoadFromFile(@”D:\Documents\Introduction.docx”, FileFormat.Docx);
            Document document2 = new Document();
            document2.LoadFromFile(@”D:\Documents\History.docx”, FileFormat.Docx);

            //Merge
            foreach(Section sec in document2.Sections)
            {
                document.Sections.Add(sec.Clone());
            }

            //Save and Launch
            document.SaveToFile(“Merge.docx”, FileFormat.Docx);
            System.Diagnostics.Process.Start(“Merge.docx”);
        }
    }
}

VB.NET:

Imports System
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace Sample
    Friend Class Merge
        Shared Sub Main(ByVal args() AsString)

            ‘Load Document
            Dim document AsNew Document()
            document.LoadFromFile(“D:\Documents\Introduction.docx”, FileFormat.Docx)
            Dim document2 AsNew Document()
            document2.LoadFromFile(“D:\Documents\History.docx”, FileFormat.Docx)

            ‘Merge
            For Each sec As Section In document2.Sections
                document.Sections.Add(sec.Clone())
            Next sec

            ‘Save and Launch
            document.SaveToFile(“Merge.docx”, FileFormat.Docx)
            System.Diagnostics.Process.Start(“Merge.docx”)
        End Sub
    End Class
End Namespace

Result Shown as Following:

Freely Download Spire.Doc for .NET

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s