【问题标题】:object of Interop Word Document Class is null on Windows Server 2008 - Word Open method互操作 Word 文档类的对象在 Windows Server 2008 上为空 - Word Open 方法
【发布时间】:2013-02-19 15:05:07
【问题描述】:

当我打开 word 文档并将其保存在我的机器上时它工作正常,但是当我将它上传到服务器上并在那里打开它时,它会进入 if (doc == null) 块,它不应该去。

如果不相关,请更新我的问题标题或要求任何澄清。

这是我的课:

using System;
using System.Collections.Generic;
using System.Web;
using Microsoft.Office.Interop.Word;

/// <summary>
/// Summary description for ClsWordExManager
/// </summary>
public class ClsWordExManager
{
     public enum Extension
        {
            WebPage = 0
        }

        private static string HtmExtension
        {
            get
            {
                return ".htm";
            }
        }

        private static Application objWordApp = null;
        private static object objMissing = System.Reflection.Missing.Value;
        private static Document doc = null;
        static ClsWordExManager()
        {
            try
            {
                objWordApp = new Application();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void InitializeClass()
        {
            objWordApp.Visible = false;
        }

        private static string Open(object strFilePath)
        {
            string str = string.Empty;
            try
            {
                objWordApp.Visible = false;
                str += "<br /> word App visiblitly false";
            }
            catch (Exception ex)
            {
                objWordApp = new Application();
                str += ex.Message;
            }
            try
            {
                doc = objWordApp.Documents.Open(ref strFilePath, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing);
                str += "<br /> word document opened";
                if (doc == null)
                {
                    // It is null when I upload it on Windows Server 2008 with office 2007 installed.
                    str += "<br /> After openging its null";
                }
            }
            catch (Exception ex)
            {
                Close();
                objWordApp.Visible = false;
                str += "<br /> word document closed with : " + ex.Message;
            }
            return str;
        }

        private static void Close()
        {
            try
            {
                doc.Close(ref objMissing, ref objMissing, ref objMissing);
            }
            catch
            {
            }
        }


        private static string SaveAs(string FilePath, string strFileExtension, WdSaveFormat objSaveFormat)
        {
            try
            {
                if (ClsCommon.IsValidUser()) // impersonating User
                {
                    FilePath = System.IO.Path.ChangeExtension(FilePath, strFileExtension);
                    try
                    {
                        if (doc != null)
                        {
                            object objFilePath = FilePath;
                            object objFormat = objSaveFormat;
                            doc.SaveAs(ref objFilePath, ref objFormat, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing);
                        }
                        else
                        {
                            FilePath += "document value is null";
                        }
                    }
                    catch
                    {
                        FilePath += "<br /> Saving document throwing expe";
                        return FilePath;
                    }

                }
                else
                {
                    FilePath += "<br /> Not valid for saving file ";
                }
            }
            catch (Exception ex)
            {
                FilePath += ex.Message;
            }
            finally
            {
                Close();
            }
            return FilePath;
        }

        public static string ReadWordFile(string strFilePath, Extension objExtension)
        {
            string strFileContent = "<br /> Reading Word File could not be completed";
            try
            {
                strFileContent += Open(strFilePath);
                if (objExtension == Extension.WebPage)
                {
                    try
                    {
                        string strNewFileName = SaveAs(strFilePath, HtmExtension, WdSaveFormat.wdFormatFilteredHTML);
                        if (strNewFileName != "")
                        {
                            strFileContent += strNewFileName + ClsCommon.ReadFile(strNewFileName, true); // ignore this line as it just read html file.
                        }
                        else
                        {
                            strFileContent += "file not saved";
                        }
                    }
                    catch (Exception ex)
                    {
                        strFileContent += ex.Message;
                    }
                }
                else
                {
                    Close();
                }
            }
            catch (Exception exx)
            {
                strFileContent += exx.Message;
            }
            return strFileContent;
        }

        public static void Quit()
        {
            try
            {
                objWordApp.Quit(ref objMissing, ref objMissing, ref objMissing);
            }
            catch
            {

            }
        }
}

【问题讨论】:

  • 提示:永远不要这样做try{...} catch (Exception ex) {throw ex;}。您最好只省略 try/catch 块。此外,ex.Message 可能会很好地显示给用户(或不显示),但如果您想知道出了什么问题,您需要 ex.ToString()。请停止使用try {...} catch{...}。出了点问题 - 不要忽视这一点。
  • 最后,这是一个什么样的应用程序?是控制台吗?如果它是一个 winhdows 服务或 ASP.NET 应用程序,那么你不能像那样使用 Office Interop。
  • @JohnSaunders 它是一个 ASP.net 应用程序,当我在答案中发布时,问题已经解决,我在调用课程之前做了模拟用户,它的工作很迷人
  • @JohnSaunders 都是好点,但是 try+catch+throw 不一定是坏的,但不要thow ex;,而只需使用throw;。所以try{...} catch (Exception ex) { throw /*no argument here*/; } 比您将所有信息传递给更高级别(或者如果您希望添加信息,请将其包装在一个新的异常中:throw new SomeMeaningFullException("with some meaningfull message", ex);

标签: c# ms-word interop windows-server-2008 office-interop


【解决方案1】:

为您的 ASP.NET 用户创建一个桌面文件夹可能已经解决了一个问题,但您会遇到更多问题。 Word可能会弹出一个对话框,你会被卡住。

办公自动化在服务器端是 explicitly unsupported,相信我 - 你会有很多的问题:

Microsoft 目前不推荐也不支持任何无人值守、非交互式客户端应用程序或组件(包括 ASP、ASP.NET、DCOM 和 NT 服务)的 Microsoft Office 应用程序自动化,因为 Office 可能表现出不稳定Office 在此环境中运行时的行为和/或死锁。

我建议您查看上面链接中引用的文章,并使用替代方案。

【讨论】:

  • 我读过那篇文章,但我怎样才能获得所有功能,如将文档保存为另一种格式 .htm 或 .pdf
  • 有许多库可以安全地执行这些功能。
【解决方案2】:

好的问题解决了感谢Sameer S post 我只是在服务器 2008 机器上创建 Desktop 文件夹@

C:\Windows\System32\config\systemprofile

现在它工作了

除此之外,因为这是ASP.NET 应用程序,所以我在调用类之前添加了impersonation 检查,它工作正常。

【讨论】:

    猜你喜欢
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 2012-01-21
    相关资源
    最近更新 更多