【问题标题】:Office automation (Interop) on Windows Server 2012Windows Server 2012 上的办公自动化(互操作)
【发布时间】:2013-01-20 15:01:57
【问题描述】:

我在 Windows Server 2008 R2 和 Office 2007 上成功使用 Office 自动化,以便将 Office 文档转换为 PDF。 代码比较简单:

public class WordConvert
{
    /// <summary>
    /// Converts a word file to PDF
    /// </summary>
    /// <param name="sourceFilePath">The path of the word file to convert</param>
    /// <param name="targetFilePath">The path of the PDF output file</param>
    public static void ConvertWord(string sourceFilePath, string targetFilePath)
    {
        object objTragetFileName = targetFilePath;
        Word.Application wordDocument = new Word.Application();
        try
        {
            OpenWord(sourceFilePath, wordDocument);
            SaveAsPDF(ref objTragetFileName, wordDocument);
        }
        finally
        {
            CloseWord(wordDocument);
        }
    }

    private static void OpenWord(object sourceFileName, Word.Application wordDocument)
    {
        wordDocument.Documents.Open(ref sourceFileName);
    }

    private static void SaveAsPDF(ref object targetFileName, Word.Application wordDocument)
    {
        object format = Word.WdSaveFormat.wdFormatPDF;
        wordDocument.ActiveDocument.SaveAs(ref targetFileName, ref format);
    }

    private static void CloseWord(Word.Application wordDocument)
    {
        if (wordDocument != null)
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();

            // 2nd time to be safe
            GC.Collect();
            GC.WaitForPendingFinalizers();

            Word.Documents documents = wordDocument.Documents;
            documents.Close();
            Marshal.ReleaseComObject(documents);
            documents = null;


            Word.Application application = wordDocument.Application;
            application.Quit();
            Marshal.ReleaseComObject(application);
            application = null;
        }
    }
}

问题是此代码在 Windows Server 2012 上不起作用。 收到的错误是:

System.Runtime.InteropServices.COMException (0x800706BA): The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

以交互式用户(控制台应用程序)身份运行代码可以正常工作,但从 IIS Web 应用程序或 Windows 服务运行时会失败(即使使用“允许服务与桌面交互”)。 运行应用程序的用户拥有足够的权限(管理员),并且代码在 Office 2010 中运行良好。

有什么想法吗?

【问题讨论】:

标签: ms-office office-interop


【解决方案1】:

我可能会因为这个答案而被否决,但我在一个企业环境中工作,我们有一个使用 Office 自动化的产品,这是非常有问题的。

我在这个领域做过研究,微软自己建议不要做办公自动化。

以下直接来自微软的MSDN知识库

Microsoft 不推荐也不支持 Office 的服务器端自动化。

还有

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

来源:http://support.microsoft.com/kb/257757

【讨论】:

  • 显然我知道这一点。但这并没有改变使用 Office 的要求。原因是 Office 最准确地将其文档转换为 pdf。
【解决方案2】:

找到的唯一解决方案是使调用 Office API 的进程以交互方式运行。 这可以通过运行控制台应用程序(对于服务器解决方案来说不是最聪明的想法)或通过创建一些后台服务(例如,windows 服务)并设置它的站(SetProcessWindowStation)来完成。

【讨论】:

    【解决方案3】:

    看到该错误消息的原因之一是服务器是否缺少 SysWow64 文件夹。这里有一个链接,可以帮助您更好地理解。

    http://per.lausten.dk/blog/2011/04/excel-automation-on-windows-server-2008-x64.html

    【讨论】:

    • 即使您的回答可能对 OP 有所帮助,您能否发布该网站的相关信息?
    猜你喜欢
    • 2014-09-28
    • 2011-11-25
    • 1970-01-01
    • 2015-05-01
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 2011-04-01
    • 2023-03-20
    相关资源
    最近更新 更多