Word Document Merger

Discussion in 'ASP.NET' started by naimish, Aug 25, 2009.

  1. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,043
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth

    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);
    			}
    		}
    	}
    }
    
     
  2. shency

    shency New Member

    Joined:
    Oct 25, 2008
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    This one will prove to be very helpful, thanks!
     
  3. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,043
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    My Pleasure :D
     
  4. Saket

    Saket New Member

    Joined:
    Jul 21, 2009
    Messages:
    42
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Don't Know
    hey, gr8 work dude, It would be nice to check out your code. let me have it. thanks.
     
  5. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,043
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    My Pleasure :D
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  7. markayi

    markayi New Member

    Joined:
    Sep 4, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.knowledge-management.org
    its great resource, thanks for sharing
     
  8. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,043
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    My Pleasure :D
     
  9. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  10. sameer_havakajoka

    sameer_havakajoka New Member

    Joined:
    Sep 14, 2009
    Messages:
    271
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Sleeping
    Location:
    Hava Ke Paro Me
    nice article buddy......!!
     
  11. hadstepre

    hadstepre New Member

    Joined:
    Sep 30, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Life is too short to be serious, laugh it up.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice