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
shency's Avatar, Join Date: Oct 2008
Go4Expert Member
This one will prove to be very helpful, thanks!
naimish like this
naimish's Avatar
Banned
My Pleasure
Saket's Avatar
Banned
hey, gr8 work dude, It would be nice to check out your code. let me have it. thanks.
naimish like this
naimish's Avatar
Banned
My Pleasure
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Nomination for Article of the month - Aug 2009 Started.
markayi's Avatar, Join Date: Sep 2009
Newbie Member
its great resource, thanks for sharing
naimish like this
naimish's Avatar
Banned
My Pleasure
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Vote for this article for Article of the month - August 2009
sameer_havakajoka's Avatar
Skilled contributor
nice article buddy......!!
naimish like this