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;
}
Sumit SaxenaPosted May 27, 2015, 6:36 AM
Alex SmithPosted Dec 2, 2014, 9:35 PM
//Load Document1 and Document2
Document DocOne = new Document();
DocOne.LoadFromFile(@"E:\Work\Document\welcome.docx", FileFormat.Docx);
Document DocTwo = new Document();
DocTwo.LoadFromFile(@"E:\Work\Document\New Zealand.docx", FileFormat.Docx);
//Merge
foreach (Section sec in DocTwo.Sections)
{
DocOne.Sections.Add(sec.Clone());
}
//Save and Launch
DocOne.SaveToFile("Merge.docx", FileFormat.Docx);
By doing so, you must add C# Word component as a reference in project.
As for the format, you need use ParagraphFromat Object to copy format from one paragraph
to another, this post may be helpful
https://social.msdn.microsoft.com/Forums/office/en-US/95fb220d-98f1-420c-b89e-2ca88e1b505b/copy-paragraphs-from-one-word-file-and-paste-it-to-another-word-file-along-with-formatting?forum=worddev
Kyle StoryPosted Nov 27, 2014, 4:29 AM
Hi, I'm not sure what your template files look like. I mean does the content of those importing files have a direct formatting or does it use some style.
In a case they have only style formatting applied to them then you can control whether you want the content to use source document's style or a destination document's style.
For example here is something that you can try:
The code uses a C# component for Word documents, it provides a simple way how you can read and modify your DOC files with C#.
Gunti DilipPosted Nov 8, 2013, 3:29 AM
Priyank KharePosted Nov 8, 2013, 3:21 AM
I think Following link will help you to get your answer-
http://devpinoy.org/blogs/keithrull/archive/2007/06/09/updated-how-to-merge-multiple-microsoft-word-documents.aspx