为什么“word文档合并”的代码在vs2008内建web服务调试器运行正常,但发布成网站访问就出错? RRS feed

  • 问题

  • 为什么相同的代码在vs2008内建web服务调试器运行正常,但发布成网站访问就出现
    IMicrosoft.Office.Interop.Word.Application.Documents.Add" 返回null值,导致程序出错?
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using OfficeManager;
    namespace webtest2
        public partial class _Default : System.Web.UI.Page
            protected void Page_Load(object sender, EventArgs e)


            protected void Button1_Click(object sender, EventArgs e)
                OfficeManager.WordClass wordclass = new WordClass();
                string[] arrfile = { @"d:\combinetest\1.doc", @"d:\combinetest\2.doc" };
                string defaultWordDocumentTemplate = @"";
                string outputfile = @"d:\combinetest\combine.doc";
                wordclass.Merge(arrfile, outputfile, false, defaultWordDocumentTemplate);


    sing Microsoft.Office.Interop.Word;
    using System.Diagnostics;
    using System.IO;
    using Microsoft.Office.Interop;

    using System.Reflection;
    namespace OfficeManager
        public class WordClass

     public  void Merge(string[] filesToMerge, string outputFilename, bool insertPageBreaks, string documentTemplate)
                object defaultTemplate = documentTemplate;
                object missing = System.Type.Missing;
                object pageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
                object outputFile = outputFilename;

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

                    // Create a new file based on our template
                    Microsoft.Office.Interop.Word._Document wordDocument = wordApplication.Documents.Add(
                                                  ref defaultTemplate
                                                , ref missing
                                                , ref missing
                                                , ref missing);

                    // Make a Word selection object.
                    Microsoft.Office.Interop.Word.Selection selection = wordApplication.Selection;

                    // Loop thru each of the Word documents
                    foreach (string file in filesToMerge)
                        // Insert the files to our template
                                                , 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, Close our Word application
                    wordApplication.Quit(ref missing, ref missing, ref missing);


    2009年9月10日 12:41


  • 1,首先,你发布的网站上必须安装Office 2003 PIA ,参考
    另外注意Office 版本


    2009年9月10日 12:54