locked
How to merge two word document into one document. RRS feed

  • Question

  • User-663629641 posted

    Hello

    I have several word documents (lets say 20)with mulitiple numbered pages that I want to merge into one long doc using C# asp.net.

    all the documents contains many screenshots and text.

    Thanks in advance.

    Shiv

     

    Friday, January 7, 2011 12:56 AM

Answers

  • User2019981500 posted

    Hi,

    you need to add a reference to Microsoft.Office.Core for this to work

    Download source code and see explaination too



    public static void Merge(string[] filesToMerge, string outputFilename, bool insertPageBreaks, string documentTemplate)
            {
                object defaultTemplate = documentTemplate;
                object missing = System.Type.Missing;
                object pageBreak = Word.WdBreakType.wdPageBreak;
                object outputFile = outputFilename;
    
                // Create  a new Word application
                Word._Application wordApplication = new Word.Application();
    
                try
                {
                    // Create a new file based on our template
                    Word._Document wordDocument = wordApplication.Documents.Add(
                                                  ref defaultTemplate
                                                , ref missing
                                                , ref missing
                                                , ref missing);
    
                    // Make a Word selection object.
                    Word.Selection selection = wordApplication.Selection;
    
                    // Loop thru each of the Word documents
                    foreach (string file in filesToMerge)
                    {
                        // Insert the files to our template
                        selection.InsertFile(
                                                    file
                                                , ref missing
                                                , ref missing
                                                , ref missing
                                                , ref missing);
    
                        //Do we want page breaks added after each documents?
                        if (insertPageBreaks)
                        {
                            selection.InsertBreak(ref pageBreak);
                        }
                    }
    
                    // Save the document to it's output file.
                    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);
    
                    // Clean up!
                    wordDocument = null;
                }
                catch (Exception ex)
                {
                    //I didn't include a default error handler so i'm just throwing the error
                    throw ex;
                }
                finally
                {
                    // Finally, Close our Word application
                    wordApplication.Quit(ref missing, ref missing, ref missing);
                }
            }





    Regards

    shabir


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 8, 2011 1:56 AM