从 VBA 打开工作簿并禁用 Workbook_Open() 代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16301530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:30:38  来源:igfitidea点击:

Open a workbook from VBA and disable Workbook_Open() code?

excelvbaexcel-vba

提问by mezamorphic

I am opening spreadsheets using VBA and a couple of the workbooks contain code which starts executing when Workbook_Open() is called.

我正在使用 VBA 打开电子表格,其中一些工作簿包含在调用 Workbook_Open() 时开始执行的代码。

How can I open the workbooks using VBA but stop the code automatically executing? I am only opening the workbooks to look at formulae in the sheet- I do not want any code execution.

如何使用 VBA 打开工作簿但停止自动执行代码?我只是打开工作簿来查看工作表中的公式 - 我不想执行任何代码。

回答by pn7a

Would you like to try disablingthe Events before you open the workbook in VBA and then re-enablingthem for the rest of the module? Try using something like this:

您想在 VBA 中打开工作簿之前尝试禁用事件,然后为模块的其余部分重新启用它们吗?尝试使用这样的东西:

Application.EnableEvents = False   'disable Events
workbooks.Open "WORKBOOKPATH"      'open workbook in question
Application.EnableEvents = True    'enable Events

回答by Raunak Thomas

I don't know why this was not clearly mentioned in the other answers but I found Application.AutomationSecurityto do exactly what was required. Basically

我不知道为什么其他答案中没有明确提到这一点,但我发现Application.AutomationSecurity完全按照要求去做。基本上

Application.AutomationSecurity = msoAutomationSecurityByUI
'This is the default behavior where each time it would ask me whether I want to enable or disable macros

Application.AutomationSecurity = msoAutomationSecurityForceDisable
'This would disable all macros in newly opened files

Application.AutomationSecurity = msoAutomationSecurityLow
'This would enable all macros in newly opened files

Even after the code is run the settings will not revert back to the default behavior so you need to change it again. Thus for this question

即使在代码运行后,设置也不会恢复到默认行为,因此您需要再次更改它。因此对于这个问题

previousSecurity = Application.AutomationSecurity
Application.AutomationSecurity = msoAutomationSecurityForceDisable
' Your code
Application.AutomationSecurity = previousSecurity

回答by 0m3r

Here another way to open with out the vba

这是另一种不用vba打开的方法

Start Excel Application > Go to File > Recent >

Start Excel Application > Go to File > Recent >

enter image description here

在此处输入图片说明

Hold Shift key and double click to open -

按住 Shift 键并双击打开 -

Doing this will prevent the Workbook_Openevent from firing and the Auto_Openmacro from running.

这样做将阻止Workbook_Open事件触发和Auto_Open宏运行。

Or hold shift key and double click to open the Workbook.

或者按住shift键双击打开工作簿。



For VBAWork with Application.EnableEvents property (Excel)

对于VBA与工作Application.EnableEvents财产(EXCEL)

回答by Daniel Davis

A combination of Application.EnableEventsand a Workbook specific Application.EnableEvents works great. Any time the workbook is re-referenced (such as copying cells) it will retrigger the activate events. The workbook has to exit first, and cant be accessed after closing so try this:

的组合Application.EnableEvents和工作簿特定Application.EnableEvents的伟大工程。任何时候重新引用工作簿(例如复制单元格),它都会重新触发激活事件。工作簿必须先退出,关闭后无法访问,所以试试这个:

Dim wb as Workbook
Application.EnableEvents = False
Set wb = workbooks.Open "YOURWORKBOOKPATH"
Application.EnableEvents = True
wb.Application.EnableEvents = False

**Code**

wb.Application.EnableEvents = True
wb.Close