How to Merge Word Documents
Microsoft Word is unarguably the best word editor around, apart from providing basic editing tools and charting styles, it also provides an option to customize document in any way you want. For example, if you always work with word documents, there comes a time that you have to merge multiple documents into one. Microsoft Word supports merging multiple Word documents into one. That function is very convenient for normal users. Here in this post you will find how to use C# to merge Word documents into one.
How to Use C# to Merge Word Documents
By using C# to merge Word documents, we don’t even need Microsoft Word installed on system. And with the help of Spire.Doc, we can do this job effortlessly.
Download Spire.Doc Here
Make sure Spire.Doc and Visual Studio are correctly installed on system. Follow the instructions below to merge word documents.
Step 1 Create Project
Create a C# windows form project in visual studio. Add Spire.Doc.dll as reference. The default setting of Spire.Doc.dll is placed under "C:\Program Files\e-iceblue\Spire.Doc\Bin”. Select assembly Spire.Doc.dll and click OK to add it to the project.Note: We still need add openFileDialog control into the project.
using System;
using System.Text;
using System.Windows.Forms;
using Spire.Doc;
using Spire.Doc.Documents;
namespace mergeword
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Step 2 Load Word Documents
Use Spire.Doc to load word documents which need be merged into one.
Load first Word document:
Document document = new Document();
document.LoadFromFile(fileName,FileFormat.Doc);
Load second Word document:
Document documentMerge = new Document();
documentMerge.LoadFromFile(fileMerge, FileFormat.Doc);
Step 3 Merge Word Documents
Use the simple code below to merge Word documents
foreach( Section sec in documentMerge.Sections)
{
document.Sections.Add(sec.Clone());
}
How to Use C# to Merge Word Documents
By using C# to merge Word documents, we don’t even need Microsoft Word installed on system. And with the help of Spire.Doc, we can do this job effortlessly.
Download Spire.Doc Here
Make sure Spire.Doc and Visual Studio are correctly installed on system. Follow the instructions below to merge word documents.
Step 1 Create Project
Create a C# windows form project in visual studio. Add Spire.Doc.dll as reference. The default setting of Spire.Doc.dll is placed under "C:\Program Files\e-iceblue\Spire.Doc\Bin”. Select assembly Spire.Doc.dll and click OK to add it to the project.Note: We still need add openFileDialog control into the project.
using System;
using System.Text;
using System.Windows.Forms;
using Spire.Doc;
using Spire.Doc.Documents;
namespace mergeword
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Step 2 Load Word Documents
Use Spire.Doc to load word documents which need be merged into one.
Load first Word document:
Document document = new Document();
document.LoadFromFile(fileName,FileFormat.Doc);
Load second Word document:
Document documentMerge = new Document();
documentMerge.LoadFromFile(fileMerge, FileFormat.Doc);
Step 3 Merge Word Documents
Use the simple code below to merge Word documents
foreach( Section sec in documentMerge.Sections)
{
document.Sections.Add(sec.Clone());
}
The following code can help us filter files (Only shows Word Doc format files) when we choose Word documents.
private string OpenFile()
{
openFileDialog1.Filter = "Word Document (*.doc)|*.doc";
openFileDialog1.Title = "Choose a document to merage";
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
return openFileDialog1.FileName;
}
return string.Empty;
}
Step 4 Save and Preview
Use the code below to save and preview
//Save doc file.
document.SaveToFile("Sample.doc", FileFormat.Doc);
//Launching the MS Word file.
WordDocViewer("Sample.doc");
private string OpenFile()
{
openFileDialog1.Filter = "Word Document (*.doc)|*.doc";
openFileDialog1.Title = "Choose a document to merage";
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
return openFileDialog1.FileName;
}
return string.Empty;
}
Step 4 Save and Preview
Use the code below to save and preview
//Save doc file.
document.SaveToFile("Sample.doc", FileFormat.Doc);
//Launching the MS Word file.
WordDocViewer("Sample.doc");
Press F5 to start the project and preview the effect.
More about 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. It supports C#, VB.NET, ASP.NET, ASP.NET MVC and Silverlight. Click to learn more…
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. It supports C#, VB.NET, ASP.NET, ASP.NET MVC and Silverlight. Click to learn more…