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

如何用VBA在Excel中添加/移除Microsoft Word 16.0对象库引用

前置操作说明
  • 先进入Excel「文件」-「选项」-「信任中心」-「信任中心设置」-「宏设置」,勾选信任对VBA工程对象模型的访问,否则代码运行会触发权限报错
  • 按下Alt+F11打开VBA编辑器,右键点击当前工作簿名称,选择「插入」-「模块」,将后续两段代码粘贴到新建的模块中
添加Microsoft Word 16.0 Object Library引用的VBA代码
Sub AddWordReference()
    On Error Resume Next
    Dim ref As Reference
    ' 校验引用是否已存在
    For Each ref In ThisWorkbook.VBProject.References
        If ref.GUID = "{00020905-0000-0000-C000-000000000046}" And ref.Major = 1 And ref.Minor = 9 Then
            MsgBox "Microsoft Word 16.0 Object Library 引用已存在", vbInformation
            Exit Sub
        End If
    Next ref
    ' 新增对应引用
    ThisWorkbook.VBProject.References.AddFromGuid _
        GUID:="{00020905-0000-0000-C000-000000000046}", _
        Major:=1, Minor:=9
    If Err.Number = 0 Then
        MsgBox "Microsoft Word 16.0 Object Library 引用添加成功", vbInformation
    Else
        MsgBox "引用添加失败,错误信息:" & Err.Description, vbCritical
    End If
    On Error GoTo 0
End Sub
移除Microsoft Word 16.0 Object Library引用的VBA代码
Sub RemoveWordReference()
    On Error Resume Next
    Dim ref As Reference
    ' 查找对应引用并移除
    For Each ref In ThisWorkbook.VBProject.References
        If ref.GUID = "{00020905-0000-0000-C000-000000000046}" And ref.Major = 1 And ref.Minor = 9 Then
            ThisWorkbook.VBProject.References.Remove ref
            MsgBox "Microsoft Word 16.0 Object Library 引用已移除", vbInformation
            Exit Sub
        End If
    Next ref
    If Err.Number <> 0 Then
        MsgBox "引用移除失败,错误信息:" & Err.Description, vbCritical
    Else
        MsgBox "未找到对应的Microsoft Word 16.0 Object Library引用", vbInformation
    End If
    On Error GoTo 0
End Sub
绑定代码到工作表按钮
  • 回到Excel工作表界面,点击「开发工具」选项卡-「插入」-「表单控件」分类下的「按钮」
  • 在工作表上拖动绘制按钮,松开鼠标后在弹出的「指定宏」窗口中选择对应宏(AddWordReference对应添加按钮,RemoveWordReference对应移除按钮),点击确定即可
  • 右键按钮可修改显示名称、调整样式,点击按钮即可执行对应操作

内容的提问来源于stack exchange,提问作者Armando Cerdeira

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 01:24:03