none
Merge Two Word Documents in C# RRS feed

  • Question

  • Hi ,
    I am developing a Word VSTO add-in. I want to merge the old document with the active Document with all the contents of both documents.
    I've tried but it returns only the old document content.
    Here is my code .
      public void Merge() {
                string file1 = @"D:\\Docx\\doc1.docx";
                string file2 = @"D:\\Docx\\doc2.docx";
                object outputFile  = @"D:\\Docx\mergedDoc.docx";
                object missing = System.Type.Missing;
                object pageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
    
                Microsoft.Office.Interop.Word._Application wordApplication = new Microsoft.Office.Interop.Word.Application();
                try
                {
                    Microsoft.Office.Interop.Word._Document wordDocument = wordApplication.Documents.Open(file1, ref missing, ref missing, ref missing, ref missing);
                    Microsoft.Office.Interop.Word.Selection selection = wordApplication.Selection;
                    wordDocument.Merge(file2, ref missing, ref missing, ref missing, ref missing);
                    wordDocument.SaveAs(
                               ref outputFile
                               , ref missing
                               , ref missing
                               , ref missing
                               , ref missing
                               , ref missing
                               , ref missing
                               , ref missing
                               , ref missing
                               , ref missing
                               , ref missing
                               , ref missing
                               , ref missing
                               , ref missing
                               , ref missing
                               , ref missing);
                    wordDocument = null;
                    System.Windows.Forms.MessageBox.Show("Success");
                }
                catch (Exception e)
                {
                    System.Windows.Forms.MessageBox.Show(e.ToString());
                }
    }
    The above code runs and returns the document with the doc2 contents only .I need both the contents.
    Can anyone help me out ?


    haraiharan.t

    Thursday, January 3, 2019 6:14 AM

All replies

  • You can use GroupDocs.Merger for .NET in order to join two documents.

    string sourceFile1 = "source file 1";
    string sourceFile2 = "source file 2"; 
    Stream openFile1 = new FileStream(sourceFile1, FileMode.Open);
    Stream openFile2 = new FileStream(sourceFile2, FileMode.Open);
    List<Stream> documentStreams = new List<Stream>();
    documentStreams.Add(openFile1);
    documentStreams.Add(openFile2);
    DocumentResult result = new DocumentHandler().Join(documentStreams);
    Stream documentStream = result.Stream;
    var fileStream = File.Create("output path" + "OutPut." + result.FileFormat);
    documentStream.CopyTo(fileStream);
    documentStream.Close();

    API supports these https://docs.groupdocs.com/display/mergernet/Supported+Document+Formats file formats.


    • Edited by atirtahir Friday, May 24, 2019 10:28 AM
    Friday, May 24, 2019 10:28 AM