Go4Expert

Go4Expert (http://www.go4expert.com/)
-   ASP.NET (http://www.go4expert.com/articles/asp-net-tutorials/)
-   -   Word Document Merger (http://www.go4expert.com/articles/word-document-merger-t19161/)

naimish 25Aug2009 08:50

Word Document Merger
 

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);
            }
        }
    }
}


shency 26Aug2009 11:00

Re: Word Document Merger
 
This one will prove to be very helpful, thanks!

naimish 26Aug2009 11:06

Re: Word Document Merger
 
My Pleasure :D

Saket 26Aug2009 13:54

Re: Word Document Merger
 
hey, gr8 work dude, It would be nice to check out your code. let me have it. thanks.

naimish 26Aug2009 14:18

Re: Word Document Merger
 
My Pleasure :D

shabbir 2Sep2009 18:55

Re: Word Document Merger
 
Nomination for Article of the month - Aug 2009 Started.

markayi 4Sep2009 19:37

Re: Word Document Merger
 
its great resource, thanks for sharing

naimish 8Sep2009 08:55

Re: Word Document Merger
 
My Pleasure :D

shabbir 19Sep2009 11:41

Re: Word Document Merger
 
Vote for this article for Article of the month - August 2009

sameer_havakajoka 19Sep2009 20:56

Re: Word Document Merger
 
nice article buddy......!!


All times are GMT +5.5. The time now is 19:18.