首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Windows Server 2012上的办公自动化(Interop)

Windows Server 2012上的办公自动化(Interop)
EN

Stack Overflow用户
提问于 2013-01-20 23:01:57
回答 3查看 8.3K关注 0票数 2

我在Windows Server2008 R2和Office2007上成功地使用了Office自动化,以便将Office文档转换为PDF。代码相当简单:

代码语言:javascript
复制
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上运行。收到的错误为:

代码语言:javascript
复制
System.Runtime.InteropServices.COMException (0x800706BA): The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

作为交互式用户(控制台应用程序)运行代码运行正常,但在从IIS web应用程序或windows服务运行时会失败(即使是在与桌面交互的情况下也是如此)。运行该应用程序的用户具有足够的权限(管理员),并且代码可以在Office 2010中正常运行。

有什么想法吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-03-18 14:34:32

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

票数 0
EN

Stack Overflow用户

发布于 2013-01-20 23:08:40

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

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

以下内容直接来自Microsoft的MSDN知识库

Office不推荐也不支持

服务器端自动化。

也是

Office目前不建议也不支持从任何无人参与的、非交互的客户端应用程序或组件(包括

、ASP.NET、DCOM和NT服务)自动运行Microsoft Office应用程序,因为当Office在此环境中运行时,可能会表现出不稳定的行为和/或死锁。

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

票数 4
EN

Stack Overflow用户

发布于 2016-04-22 20:50:01

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14425803

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档