Windows Server 2012上的Office自动化(Interop) [英] Office automation (Interop) on Windows Server 2012

查看:188
本文介绍了Windows Server 2012上的Office自动化(Interop)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm successfully using Office automation on Windows Server 2008 R2 with Office 2007 in order to convert Office documents to PDFs. The code is rather simple:

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上不起作用. 收到的错误是:

The problem is that this code doesn't work on Windows Server 2012. The error received is:

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

以交互用户(控制台应用程序)运行代码可以正常工作,但是从IIS Web应用程序或Windows服务(即使具有允许服务与桌面交互")运行时,也会失败. 运行该应用程序的用户具有足够的权限(管理员),并且该代码可与Office 2010正常运行.

Running the code as interactive user (console app) works fine but it fails when running from IIS web app or from windows service (even with 'alow service to interact with desktop'). The user running the app has enough permissions (administrator) and the code works fine with Office 2010.

有什么想法吗?

推荐答案

找到的唯一解决方案是使调用Office API的过程以交互方式运行. 可以通过运行控制台应用程序(不是服务器解决方案最聪明的主意)或创建一些后台服务(例如Windows服务)并将其设置为工作站(SetProcessWindowStation)来完成此操作.

The only solution found is to make the process that invokes Office API to run as interactive. It can be done by just running a console app (not the brightest idea for server solutions) or by creating some background service (e.g., windows service) and set it's station (SetProcessWindowStation).

这篇关于Windows Server 2012上的Office自动化(Interop)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆