Gunti Dilip

Gunti Dilip

  • NA
  • 54
  • 103.9k

Able to merge multiple documents but not getting same format

Nov 8 2013 3:18 AM
Hi,

I am trying to merge multiple word documents into single word document using "InsertFile()"
But i am not getting same format as there in the multiple documents.

Could you please tell me how to get insert multiple word documents into a single word document with same format.

Code Which i am executing..

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