[英]Open a workbook from VBA and disable Workbook_Open() code?
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?如何使用 VBA 打开工作簿但停止代码自动执行? I am only opening the workbooks to look at formulae in the sheet- I do not want any code execution.
我只是打开工作簿来查看工作表中的公式 - 我不想执行任何代码。
Would you like to try disabling the Events before you open the workbook in VBA and then re-enabling them for the rest of the module? 是否要在VBA中打开工作簿之前尝试禁用事件,然后为模块的其余部分重新启用它们? Try using something like this:
尝试使用这样的东西:
Application.EnableEvents = False 'disable Events
workbooks.Open "WORKBOOKPATH" 'open workbook in question
Application.EnableEvents = True 'enable Events
I don't know why this was not clearly mentioned in the other answers but I found Application.AutomationSecurity
to do exactly what was required. 我不知道为什么在其他答案中没有明确提到这一点,但我发现
Application.AutomationSecurity
完全按照要求做了。 Basically 基本上
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
Here another way to open with out the vba 这里有另一种打开vba的方法
Start Excel Application > Go to File > Recent >
Hold Shift key and double click to open - 按住Shift键并双击打开 -
Doing this will prevent the Workbook_Open
event from firing and the Auto_Open
macro from running. 执行此操作将阻止触发
Workbook_Open
事件并运行Auto_Open
宏。
Or hold shift key and double click to open the Workbook. 或按住Shift键并双击打开工作簿。
For
VBA
Work with Application.EnableEvents property (Excel)对于
VBA
使用Application.EnableEvents属性(Excel)
A combination of Application.EnableEvents and a Workbook specific Application.EnableEvents works great. Application.EnableEvents和特定于Workbook的Application.EnableEvents的组合非常有效。 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:
工作簿必须先退出,并且在关闭后无法访问,请尝试以下操作:
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
Open an empty excel macro file and put a macro with Application.EnableEvents = False, And then open the file.打开一个空的excel宏文件,放一个Application.EnableEvents = False的宏,然后打开文件。 After that do Application.EnableEvents = True.
之后做 Application.EnableEvents = True。
Dim xlapp as New Excel.Application将 xlapp 调暗为新的 Excel.Application
Dim wb as Workbook将 wb 调暗为工作簿
xlapp.EnableEvents = False xlapp.EnableEvents = 假
Set wb = xlapp.Workbooks.Open("My workbook name") Set wb = xlapp.Workbooks.Open("我的工作簿名称")
'Process the workbook '处理工作簿
wb.Close savechanges:= false wb.Close 保存更改:= false
xlapp.Quit xlapp.退出
set wb = nothing设置 wb = 无
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.