Word Document Merger

naimish's Avatar author of Word Document Merger
This is an article on Word Document Merger in ASP.NET.
Rated 3.00 By 2 users

Introduction



Have you ever face the problem of merging so many word docs into a single one ?!!!
Heres the solution

Merge multiple word documents into a single document using .Net code.

Background



This asset is used to merge multiple word documents into a single document. If you need to merge the word document dynamically through code, this tool will help to complete this task in a very simple manner.

The code



Code:
//Word Document Merger
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
//For office interop
using Microsoft.Office.Interop;
using Microsoft.Office.Core;

namespace WordDocMerger
{
    public partial class frmMerger : Form
    {
        public frmMerger()
        {
            InitializeComponent();
        }
        private void btnMerge_Click(object sender, EventArgs e)
        {
            try
            {
                //For application root directory
                string rootDir = AppDomain.CurrentDomain.BaseDirectory.ToString();
                //Soruce, Output document names
                object docName1 = rootDir + "Doc1.doc";
                object docName2 = rootDir + "Doc2.doc";
                object docMerged = rootDir + "MergedDocument.Doc";

                //Create a Word App
                Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
                wordApp.Visible = false;
                //Interop params
                object oMissing = System.Reflection.Missing.Value;
                object save = true;
                object notSave = false;
                //For input file validations
                if (!File.Exists(docName1.ToString()))
                {
                    MessageBox.Show("Source Document1 does not exist !","Failed !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (!File.Exists(docName2.ToString()))
                {
                    MessageBox.Show("Source Document2 does not exist !","Failed !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (File.Exists(docMerged.ToString()))
                {
                    File.Delete(docMerged.ToString());
                }
                //Opening the document for processing
                Microsoft.Office.Interop.Word.Document wordDoc2 = wordApp.Documents.Open(ref docName2, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                Microsoft.Office.Interop.Word.Document wordDoc1 = wordApp.Documents.Open(ref docName1, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                //Find the range to copy
                Microsoft.Office.Interop.Word.Range range;// Range for the string in the document
                //Get the range of the current active document
                range = wordApp.Selection.Range;
                //start range is 0; end range is any max value of type interger
                range.SetRange(0, 999999999);

                //Copy the entire entire document
                range.Copy();
                wordDoc1.Close(ref notSave, ref oMissing, ref oMissing);
                //Get the range of the 2nd document to pate
                range = wordApp.Selection.Range;
                range.SetRange(0, 0);

                //Paste copied content into another document
                range.PasteAndFormat(Microsoft.Office.Interop.Word.WdRecoveryType.wdFormatOriginalFormatting);
                Clipboard.Clear();
                //Save the Mergerd document into new file
                wordDoc2.SaveAs(ref docMerged, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                //close the doc objects
                wordDoc2.Close(ref notSave, ref oMissing, ref oMissing);
                wordApp.Quit(ref notSave, ref oMissing, ref oMissing);
                //Release the memory of COM components
                System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
                MessageBox.Show("Document Merged Sucessfully !. Please Check 'MergerDocument.doc' file.", "Sucess !", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Failed !", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
markayi, night.rider likes this
Go4Expert Member
26Aug2009,11:00   #2
shency's Avatar
This one will prove to be very helpful, thanks!
naimish likes this
Banned
26Aug2009,11:06   #3
naimish's Avatar
My Pleasure
Banned
26Aug2009,13:54   #4
Saket's Avatar
hey, gr8 work dude, It would be nice to check out your code. let me have it. thanks.
naimish likes this
Banned
26Aug2009,14:18   #5
naimish's Avatar
My Pleasure
Go4Expert Founder
2Sep2009,18:55   #6
shabbir's Avatar
Nomination for Article of the month - Aug 2009 Started.
Newbie Member
4Sep2009,19:37   #7
markayi's Avatar
its great resource, thanks for sharing
naimish likes this
Banned
8Sep2009,08:55   #8
naimish's Avatar
My Pleasure
Go4Expert Founder
19Sep2009,11:41   #9
shabbir's Avatar
Vote for this article for Article of the month - August 2009
Skilled contributor
19Sep2009,20:56   #10
sameer_havakajoka's Avatar
nice article buddy......!!
naimish likes this