Most of us work with Word document often and sometimes we indeed need merge multiple word documents into one master file. To merge word documentsinto one master file could be very easy although Word doesn’t have a directly import/export function to do that.

Merge Word Documents in Microsoft Word

If we just need merge 2 or 3 word documents, we can easily use Word “insert” function. Open the first Word document and just insert the second document at the end of the first word document content by clicking “file” from tool bar “insert” category. Then, repeat this if you want to merge the third document into the master file. At last, save it. This function enable us select multiple word documents by press “Ctrl” or “Shift” and merge them into one master word file. But the top file you select will be merged first. So if there is an order that you want to maintain, make sure to rename the files first so that the appearance in order in the dialog box.

Note: Sometimes when you merge Word documents this way, the formatting does not always the same as the original. So, you may manually fix it.

Merge Word Documents via Spire.Doc

Spire.Doc is a MS Word component which enables user to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document for .NET and Silverlight. We can use Spire.Doc to merge multiple Word documents into one master document via C#/VB.NET. Through this solution, we do not need Microsoft Word installed on our system. Download Spire.Doc here and use the code below to merge Word documents. 

C# Merge Word Documents:

01    private void button1_Click(object sender, EventArgs e)
02    {
03        string fileName = OpenFile();
04        string fileMerge = OpenFile();
05        if ((!string.IsNullOrEmpty(fileName)) && (!string.IsNullOrEmpty(fileMerge)))
06        {
07            //Create word document
08            Document document = new Document();
09            document.LoadFromFile(fileName,FileFormat.Doc);
10   
11            Document documentMerge = new Document();
12            documentMerge.LoadFromFile(fileMerge, FileFormat.Doc);
13   
14            foreach( Section sec in documentMerge.Sections)
15            {
16                document.Sections.Add(sec.Clone());
17            }
18   
19            //Save doc file.
20            document.SaveToFile("Sample.doc", FileFormat.Doc);
21   
22            //Launching the MS Word file.
23            WordDocViewer("Sample.doc");
24        }
25   
26   
27    }
28   
29    private string OpenFile()
30    {
31        openFileDialog1.Filter = "Word Document (*.doc)|*.doc";
32        openFileDialog1.Title = "Choose a document to merage";
33   
34        openFileDialog1.RestoreDirectory = true;
35        if (openFileDialog1.ShowDialog() == DialogResult.OK)
36        {
37            return openFileDialog1.FileName;
38        }
39   
40        return string.Empty;
41    }
42   
43    private void WordDocViewer(string fileName)
44    {
45        try
46        {
47            System.Diagnostics.Process.Start(fileName);
48        }
49        catch { }
50    }

 

VB.NET Merge Word Documents:

01    Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
02        Dim fileName As String = OpenFile()
03        Dim fileMerge As String = OpenFile()
04        If ((Not String.IsNullOrEmpty(fileName))) AndAlso ((Not String.IsNullOrEmpty(fileMerge))) Then
05            'Create word document
06            Dim document_Renamed As New Document()
07            document_Renamed.LoadFromFile(fileName,FileFormat.Doc)
08   
09            Dim documentMerge As New Document()
10            documentMerge.LoadFromFile(fileMerge, FileFormat.Doc)
11   
12            For Each sec As Section In documentMerge.Sections
13                document_Renamed.Sections.Add(sec.Clone())
14            Next sec
15   
16            'Save doc file.
17            document_Renamed.SaveToFile("Sample.doc", FileFormat.Doc)
18   
19            'Launching the MS Word file.
20            WordDocViewer("Sample.doc")
21        End If
22   
23   
24    End Sub
25   
26    Private Function OpenFile() As String
27        openFileDialog1.Filter = "Word Document (*.doc)|*.doc"
28        openFileDialog1.Title = "Choose a document to merage"
29   
30        openFileDialog1.RestoreDirectory = True
31        If openFileDialog1.ShowDialog() = DialogResult.OK Then
32            Return openFileDialog1.FileName
33        End If
34   
35        Return String.Empty
36    End Function
37   
38    Private Sub WordDocViewer(ByVal fileName As String)
39        Try
40            Process.Start(fileName)
41        Catch
42        End Try
43    End Sub
 

Click here to learn More about Spire.Doc

Share:
  • Facebook
  • Twitter
  • Digg
  • Posterous
  • Add to favorites
  • Kirtsy
  • Ping.fm
  • IndianPad

Leave a Reply

Categories

Learn Chinese

Date

September 2011
M T W T F S S
« Aug   Oct »
 1234
567891011
12131415161718
19202122232425
2627282930  

Twiiter

Visits

90
Unique
Visitors
Google Analytics
Stop censorship