You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Microsoft Interop库转换Word为PDF时遭遇权限错误求助

Word转PDF时Interop库出现E_ACCESSDENIED权限错误的解决方案

问题背景

我们正在开发文件转换功能,尝试使用Microsoft Interop库将Word文档(doc及docx格式)转换为PDF文件,实现的代码片段如下:

public string Get()
{
    try
    {
        Microsoft.Office.Interop.Word.Application appWord = new 
        Microsoft.Office.Interop.Word.Application();

        wordDocument = appWord.Documents.Open(@"<Document Path>");
        wordDocument.ExportAsFixedFormat(@"<Document Path>", 
                                         WdExportFormat.wdExportFormatPDF);
       return "converted";
    }
    catch (Exception ex) 
    {
        string error = ex.Message.ToString();
        return error;
    }         
}
public Microsoft.Office.Interop.Word.Document wordDocument { get; set; }

通过REST API调用该代码时,出现以下权限错误:

Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).

开发环境

  • 操作系统:Microsoft Windows 10 Pro
  • 代码编辑器:Visual Studio 2022
  • 技术栈:C#(.Net Framework 4.6.2)
  • Office版本:Microsoft Office 365(32 bit)

注:必须仅使用Interop库,不可使用第三方DLL


解决方案

1. 配置DCOM组件权限

这是导致该权限错误的核心原因,REST API通常运行在IIS应用池账户或服务账户下,需给该账户赋予Word COM组件的访问权限:

  • 按下Win+R输入dcomcnfg打开组件服务,展开组件服务 > 计算机 > 我的电脑 > DCOM配置
  • 若系统是64位但Office是32位,需运行mmc -32打开32位控制台,再添加「组件服务」管理单元,否则无法找到对应Word组件
  • 在DCOM配置列表中找到Microsoft Word Document(部分版本可能显示为Microsoft Word 97 - 2003 Document),右键选择属性
  • 切换到「安全」选项卡:
    • 在启动和激活权限区域选择「自定义」,点击「编辑」,添加你的API应用池账户(格式:IIS AppPool\[你的应用池名称]),勾选「本地启动」和「本地激活」权限
    • 在访问权限区域做相同操作,添加该账户并勾选「本地访问」权限
  • 切换到「标识」选项卡,选择「交互式用户」;若API运行在服务账户下,可选择「此用户」并指定拥有足够权限的账户

2. 确保文件路径权限

  • 检查<Document Path>对应的源文件目录和PDF输出目录,给应用池账户赋予读写权限,避免因文件访问权限不足引发错误

3. 匹配平台位数

  • 在Visual Studio中打开项目属性,切换到「生成」选项卡,将平台目标设置为x86,确保和32位Office的位数一致,避免跨位数COM调用问题

4. 修复代码中的资源泄漏问题

原代码未正确关闭Word进程,会导致进程残留并可能引发后续权限问题,修改代码如下:

public string Get()
{
    Microsoft.Office.Interop.Word.Application appWord = null;
    Microsoft.Office.Interop.Word.Document wordDocument = null;
    try
    {
        appWord = new Microsoft.Office.Interop.Word.Application();
        // 注意:输出路径需与源文件路径区分,避免覆盖原文档
        wordDocument = appWord.Documents.Open(@"<源Word文件路径>");
        wordDocument.ExportAsFixedFormat(@"<PDF输出文件路径>", WdExportFormat.wdExportFormatPDF);
        return "converted";
    }
    catch (Exception ex)
    {
        return ex.Message;
    }
    finally
    {
        // 释放COM资源并关闭Word进程
        if (wordDocument != null)
        {
            wordDocument.Close(false);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(wordDocument);
        }
        if (appWord != null)
        {
            appWord.Quit(false);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(appWord);
        }
    }
}

内容的提问来源于stack exchange,提问作者Ruby Sharma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 22:10:29