locked
How to merge more than one doc file into any single file format (like doc, html etc) in asp.net RRS feed

  • Question

  • User1535205523 posted

    I am using this code for merge file, this code working fine in window application but not working in web application.

    through exception is "Object reference not set to an instance of an object."

    plz help me

    public static void Merge(string[] filesToMerge, string outputFilename, bool insertPageBreaks, string documentTemplate)
        {
            object defaultTemplate = documentTemplate;
            object missing = System.Reflection.Missing.Value;
            object pageBreak = Word.WdBreakType.wdPageBreak;
            object outputFile = outputFilename;


            // Create  a new Word application
            Word._Application wordApplication = new Word.Application();

            //Word.ApplicationClass oWordApplic;


            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
                    //oWordApplic  = new Word.ApplicationClass();
                    //oWordApplic.Selection.TypeText("sdfdsfds");

                    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)
            {
                
            }
            finally
            {
                // Finally, Close our Word application
                wordApplication.Quit(ref missing, ref missing, ref missing);
            }
        }

    Tuesday, November 8, 2011 6:03 AM

All replies