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

MacOS下通过代码打开含宏工作簿时跳过宏启用/禁用提示的方法

Mac版Excel如何通过代码打开带宏工作簿并跳过宏启用提示?

我希望通过代码打开包含宏的工作簿,同时跳过默认弹出的“启用/禁用宏”提示。尝试了多种方法都无效,想找适用于Mac的解决方案。

环境信息

  • Excel 16.78.3(2023年10月)
  • VBA 7.1(2012版)
  • macOS Sonoma 14.0
  • 13英寸M2芯片MacBook Pro

注意事项

  1. 是不是因为Mac版Office没有信任中心才解决不了?
  2. 知道可以通过「Microsoft Excel > 偏好设置 > 安全性 > 启用所有宏」跳过提示,但这个方法会允许所有工作簿无提示运行宏,风险太高,不想用。

我写的测试代码如下:

Private Sub tst()
    
    '保存当前参数值
    secAutomation = Application.AutomationSecurity
    alertsDisplay = Application.DisplayAlerts
    eventsEnabled = Application.EnableEvents
    
    '======================
    '尝试设置以跳过宏提示
    Application.AutomationSecurity = msoAutomationSecurityLow
    Application.DisplayAlerts = False '虽说明对AutomationSecurity无效,但还是试一下
    Application.EnableEvents = False '同样尝试一下
    
    Workbooks.Open FileName:=ActiveWorkbook.Path & "/Book2.xlsm", UpdateLinks:=3
    '问题:仍然弹出启用/禁用宏的提示
    '======================
    
    '恢复原设置
    Application.EnableEvents = eventsEnabled
    Application.DisplayAlerts = alertsDisplay
    Application.AutomationSecurity = secAutomation

End Sub 'tst

解决方案

Mac版Excel确实没有Windows版的信任中心,但可以通过以下两种相对安全的方式解决:

方法1:添加受信任位置

打开Excel的「偏好设置 > 安全性 > 受信任位置」,点击「+」按钮添加目标工作簿所在的文件夹。添加后,该文件夹内的带宏工作簿会被自动信任,打开时不会弹出宏提示,其他位置的工作簿仍会正常触发提示,平衡安全性和需求。

方法2:VBA结合AppleScript打开

如果不想添加整个文件夹为受信任位置,可以用AppleScript绕过提示,在VBA中调用执行:

Private Sub OpenWorkbookWithoutMacroPrompt()
    Dim scriptStr As String
    Dim filePath As String
    
    '保存当前自动化安全设置
    Dim secAutomation As MsoAutomationSecurity
    secAutomation = Application.AutomationSecurity
    Application.AutomationSecurity = msoAutomationSecurityLow
    
    '目标工作簿路径
    filePath = ActiveWorkbook.Path & "/Book2.xlsm"
    '构建AppleScript命令
    scriptStr = "tell application ""Microsoft Excel"" to open POSIX file """ & filePath & """ with macros enabled"
    
    '执行AppleScript
    MacScript scriptStr
    
    '恢复原设置
    Application.AutomationSecurity = secAutomation
End Sub

这个方法通过AppleScript的with macros enabled参数直接启用宏打开工作簿,不会弹出提示,且无需修改全局宏安全设置。

补充说明

你之前代码中使用的msoAutomationSecurityLow在Mac版Excel中对VBA打开的工作簿无效,这是Mac版Office的API限制,因此需要用上述两种方法替代。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 22:05:32