【问题标题】:Error when calling Word from Excel: The macros in this project are disabled从 Excel 调用 Word 时出错:此项目中的宏已禁用
【发布时间】:2020-07-14 18:03:11
【问题描述】:

我在 Excel 中有 VBA 代码,它调用指定本地文件夹中的 Word 文件。

对于某些用户,它会导致以下错误:

此项目中的宏已禁用。请参考宿主应用程序的在线帮助或文档来确定如何启用宏

出现错误是因为代码应该从 Excel 移动到 Word 文件。

到目前为止我已经尝试过:
Excel 信任中心:

  • 整个位置(包括受信任的子文件夹)。
  • 选中“允许网络上的文档受信任”。
  • 禁用所有宏并通知已“选中”。
    我无法更改它,因为它是灰色的。但是,此设置对所有用户都是相同的。
  • 受保护的视图已禁用。

Word 信任中心

  • 受保护的视图已禁用。
  • 打开 Word 文件不会产生任何“启用宏”通知。

【问题讨论】:

  • 您好,您找到解决方法了吗?我也有同样的问题。
  • 也许您可以将 VBA 代码设置为加载项,这样它与 Excel 实例的连接比与工作簿/文档的连接更紧密。这是一个可能的解决方案吗? @格里芬
  • 当您说“代码应该从 Excel 移动到 Word 文件时出现错误”,我们是否应该理解文档中有一段代码(.docm 类型),还是在 Normal.dotm 中?或者,excel 代码只在 Word 会话中操作文档?
  • 这可能是由于启用宏选项的原因,尝试将其集成到您的excel中,以便自动启用宏xl-central.com/force-users-to-enable-macros-in-a-workbook.html
  • @user19702 的回答中提出的一些问题是有效的。具体来说,您需要提供有关每个用户的 MS Office 和 Windows 环境的更多详细信息。有些运行 32 位,有些运行 64 位?有些运行 Office 2013,有些运行 2016 或 2019 或 365?是否有一些运行 C2R 而另一些运行 MSI?这些是您在这种情况下必须澄清的事情。

标签: excel vba ms-word


【解决方案1】:

前段时间我也遇到过类似的问题。从 Excel 宏中打开 word 文档时,对我来说一切正常。但在另一台 PC 上,宏只是停止了,并显示一条消息指示宏被禁用。

可以通过将单词 app 的 Application.AutomationSecurity 属性更改为 msoAutomationSecurityLow 来解决此问题。

在代码执行后将该属性设置回其原始值很重要。

您可以尝试以下代码示例。

Option Explicit

Sub OpenWordsFilePathWithLowSecuritySettings()
    Dim sFilePath As String
    Dim wrdApp As Object
    Dim wrdDoc As Object
    Dim lAutomationSetting As Long
    
    'The path to your word file
    sFilePath = "C:\Users\micha\Desktop\example file.docx"

    Set wrdApp = CreateObject("Word.Application")
    
    wrdApp.Visible = True
    
    'Save word app automation security so we can restore it afterwards
    lAutomationSetting = wrdApp.AutomationSecurity
    'Error handling to make sure the automation security is reset even if an error occurs
    On Error GoTo ErrorHandler
    'Change the automation setting to low security
    wrdApp.AutomationSecurity = msoAutomationSecurityLow
    
    'Open word document
    Set wrdDoc = wrdApp.Documents.Open(sFilePath)
    
    'Your code - do something with the word file
    '
    '
    '
    
ErrorExit:
    On Error Resume Next
    'Close the word document
    wrdDoc.Close
    'Reset the word automation security
    wrdApp.AutomationSecurity = lAutomationSetting
    wrdApp.Quit
    Set wrdDoc = Nothing
    Set wrdApp = Nothing
    
    Exit Sub
    
ErrorHandler:
    MsgBox "An error occured: (Code: " & Err.Number & ", Description: " & Err.Description & ")", vbCritical, "Error"
    Resume ErrorExit
End Sub

【讨论】:

    【解决方案2】:

    故意将其灰显(也许您的用户的 IT 通过 gpo 设置?)的正常方法是注册表项,其中 16.0 是您安装的版本

    [HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Excel\Security]
    "VBAWarnings"=dword:00000001
    

    其他一些需要解决的问题:

    • 他们可以创建和运行自己的宏吗?
    • 他们可以在不同的文档中运行不同的宏吗?
    • 有人有不同版本的 Excel 吗?
    • excel 和 word 文件是否都是用户 PC 的本地文件?
    • 用户是否启用了开发者设置?
    • 是否有人在宏设置 > 开发者宏设置中有不同的设置?
    • Windows 是否阻止了 excel 文件?右键单击文件 > 属性 > 常规:

    还有this specific GPO 只阻止来自松散定义的“Internet”的宏

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-19
      • 2021-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-12
      • 1970-01-01
      • 2018-08-30
      相关资源
      最近更新 更多