5
Answers

Merge Multiple Word Documents in C# With Same Format

Gunti Dilip

Gunti Dilip

9y
15.5k
1
Hi All,

I am able to Merge multiple word documents into a single word document but i am not getting the same format  ?

Could u please help me out to get the same format.

Code:
   Word.Application objWordApp = new Word.Application();
            Word.Document objWordDoc = new Word.Document();
            object missing = Type.Missing;
            object visible = true;
            object objCreateDoc = "E:\\MergeTemplates\\Temp.doc";
            string strNewDocPath1 = "E:\\MergeTemplates\\Template1.doc";
            string strNewDocPath2 = "E:\\MergeTemplates\\Template2.doc";
            string strNewDocPath3 = "E:\\MergeTemplates\\Template3.doc";

            object objPageBreak = Word.WdBreakType.wdPageBreak;

            try
            {

                objWordDoc = objWordApp.Documents.Open(objCreateDoc, false, false, ref missing,
                                                    ref missing, ref missing, ref missing, ref missing,
                                                    ref missing, ref missing, ref missing, true,
                                                    ref missing, ref missing, ref missing, ref missing);
                
                objWordDoc.Activate();
                objWordApp.Selection.InsertFile(strNewDocPath1, ref missing, true, ref missing, ref missing);
                objWordApp.Selection.InsertBreak(ref objPageBreak);
                objWordApp.Selection.InsertFile(strNewDocPath2, ref missing, ref missing, ref missing, ref missing);
                objWordApp.Selection.InsertBreak(ref objPageBreak);
                objWordApp.Selection.InsertFile(strNewDocPath3, ref missing, ref missing, ref missing, ref missing);
                objWordApp.Selection.InsertBreak(ref objPageBreak);
                objWordApp.ActiveDocument.SaveAs(objCreateDoc, ref missing, ref missing,
                                                  ref missing, ref missing, ref missing, ref missing,
                                                  ref missing,ref missing, ref missing, ref missing);
                objWordApp.ActiveDocument.Close(ref missing, ref missing, ref missing);              
            }
            catch (Exception ex)
            {
                throw ex;
            }
Answers (5)