none
为什么“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 = @"Normal.dot";
                string outputfile = @"d:\combinetest\combine.doc";
                wordclass.Merge(arrfile, outputfile, false, defaultWordDocumentTemplate);
            }

         }
    }
     ----------------------------------
    wordclass.cs代码如下:

    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();

                try
                {
                    // 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
                        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);
                }
            }

    }

    2009年9月10日 12:41

答案

  • 1,首先,你发布的网站上必须安装Office 2003 PIA ,参考
    http://msdn.microsoft.com/zh-cn/library/aa159923%28office.11%29.aspx
    另外注意Office 版本
    2007的下载
    http://www.microsoft.com/downloads/details.aspx?FamilyID=59daebaa-bed4-4282-a28c-b864d8bfa513&displaylang=en

    2,服务器上必须设置相应的权限,允许匿名账户执行

    【孟子E章】
    2009年9月10日 12:54
    版主