PAYATHON 2026

VSTO Word 加载项报 0x800A16C1 Object has been deleted 错误排查

技术问小赵

结论

这个异常通常意味着 VSTO 持有的 Word Document COM 对象(RCW)已经失效,普通空引用的表现通常不是这样。从你描述的现象看,两个触发点都发生在 Save() 之后,日志里 UpdateTagsIfBroken/DocumentOpen 也有重入迹象。比较可能的过程是:程序化保存引起文档重载、事件再次进入,或者 OneDrive 同步/自动保存相关处理替换、释放了原先缓存的 Word.Document,后续再访问 Fields、FullName 时就抛出 0x800A16C1。Office 2607 和 OneDrive 共享本地位置都是重要诱因,但不能据此直接判断为官方 bug,仍要按下面的方法验证。

原因拆解

  1. Save() 后继续使用旧文档引用。Word 在保存、同步、受保护视图、加载项事件等情况下可能重新加载文档,旧 RCW 不会自动更新。
  2. 在 Application_DocumentOpen 里同步保存或刷新大量 fields/tags,容易让事件重入。日志中 UpdateTagsIfBroken 再次出现,说明旧流程可能又被调用了一次。
  3. 同一个 Word.Application 实例缓存了失效的文档对象或字段集合,结果一个文档出错后,其他文档也跟着持续报错。
  4. OneDrive 共享本地位置、auto save 设置和 Office 2607 会影响保存时序,但还需要通过最小化对比来确认。

修复步骤

1. 保存前缓存路径,保存后不要立刻读旧对象

临时副本场景可以改成下面这样:

if (this.wordDocument.Path.Length > 0)
{
    string templatePath = this.wordDocument.FullName; // 保存前取
    this.wordDocument.Save();

    // 不要再读 this.wordDocument.FullName
    copy = tools.GetDocument(
        tools.WordApp.Documents.Add(Template: templatePath, Visible: visible));
}

如果后续还要操作原文档,保存后用路径重新获取:

private static Word.Document ReacquireDocument(Word.Application app, string fullName)
{
    foreach (Word.Document d in app.Documents)
    {
        try
        {
            if (string.Equals(d.FullName, fullName, StringComparison.OrdinalIgnoreCase))
                return d;
        }
        catch (COMException)
        {
            // 遍历期间文档可能仍在变化,单个文档失败不应中断
        }
    }
    return null;
}

2. 给 DocumentOpen 加防重入和延迟保存

不要在 Application_DocumentOpen 事件里同步执行 doc.Save()。先处理标签/变量,再延迟保存,并记录当前正在处理的文档 key:

private static readonly HashSet<string> _opening =
    new HashSet<string>(StringComparer.OrdinalIgnoreCase);

public void Application_DocumentOpen(Word.Document doc)
{
    string key = null;
    try
    {
        key = doc.FullName;
        if (!_opening.Add(key))
            return; // 防重入

        ABC.Application app = new ABC.Application();
        app.Tools.DocumentOpen(doc);

        if (app.Tools.ReqDocumentSave)
        {
            isDocumentSaveWhenOpen = true;

            var docKey = key;
            SynchronizationContext.Current?.Post(_ =>
            {
                try
                {
                    var current = ReacquireDocument(Globals.ThisAddIn.Application, docKey);
                    current?.Save();
                }
                catch (COMException ex)
                {
                    // 记录 HRESULT,不要无限重试
                }
            }, null);
        }
    }
    catch (COMException ex) when (ex.HResult == unchecked((int)0x800A16C1))
    {
        // 旧 doc 已失效,记录并让本轮退出,必要时重新获取一次
    }
    finally
    {
        if (key != null)
            _opening.Remove(key);
    }
}

注意:如果项目里没有合适的 SynchronizationContext,可以用 VSTO/Windows Forms 的消息循环做延迟。关键点是让事件先返回,再保存,不要在 DocumentOpen 内同步保存。

3. 封装 0x800A16C1 的安全访问

捕获这个异常后,不要继续使用旧对象。建议重新获取文档,然后只重试一次:

private static bool IsObjectDeleted(COMException ex)
{
    return ex.HResult == unchecked((int)0x800A16C1);
}

在 UpdateTagsIfBroken 内部,每次刷新 fields 前重新拿当前文档,不要跨 Save() 持有 Fields 集合:

var current = ReacquireDocument(app, documentKey);
if (current == null) return;

foreach (Word.Field field in current.Fields)
{
    // 处理 field
}

4. 清理跨文档缓存

在 DocumentOpen、DocumentClose 时移除缓存,不要把 Word.Document、Fields、Range 长期放在静态字段里。文档对象一旦失效,同一个 Word.Application 实例就会持续报错,通常都是旧引用没有清干净。

5. 做环境对比验证

  • 把同一文档复制到普通本地目录,排除 OneDrive 同步的影响;
  • 临时关闭 AutoSave 和 OneDrive 同步后再试;
  • 在条件允许时回滚或切换 Office 2607 到其他稳定版本做对比;
  • 记录 Word build、通道、OneDrive 客户端版本、文档是否从服务器下载、是否发生重载;
  • 检查受保护视图、信任中心和加载项冲突。 这些验证不能代替代码修复,但能帮助确认诱因。

注意事项

  • 0x800A16C1 出现后,旧 Word.Document 通常不能继续使用。重新获取对象比反复重试更可靠。
  • 不要在 DocumentOpen 中做大量 COM 调用和同步保存,尤其不要刷新 fields 后立刻保存,然后又去访问旧 fields。
  • 不要把无限重试、GC.Collect() 或频繁 ReleaseComObject 当作常规修复手段。
  • 如果回滚 Office 版本后问题消失,可以向 Microsoft 提交反馈,附上 build、日志和复现步骤。但在没有官方结论前,不要断言根因。

备注:内容仅供参考。