Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

excel

vba

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

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.

like image 554
mezamorphic Avatar asked Apr 30 '13 13:04

mezamorphic


People also ask

How do I turn off workbook open event?

Start Excel Application > Go to File > Recent > Doing this will prevent the Workbook_Open event from firing and the Auto_Open macro from running.

How do I open an Excel file without triggering the macro?

To run a workbook without triggering a startup macro, you need to open it from within Excel, rather than double-clicking the file in Windows. Open Excel, go to the File menu, click "Open" and locate your file. Hold down the "Shift" key while you click "Open," and continue holding it until the workbook finishes loading.

How do I disable part of a VBA code?

Steps to disable the VBA macroClick on Tools > Visual Basic for Application. From the VB Project window, select the desired module which contains the VBA code. Use the mouse (press and hold left-click button) to highlight all the code that you would like disabled.


Video Answer


3 Answers

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? Try using something like this:

Application.EnableEvents = False   'disable Events
workbooks.Open "WORKBOOKPATH"      'open workbook in question
Application.EnableEvents = True    'enable Events
like image 129
pn7a Avatar answered Sep 20 '22 01:09

pn7a


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. 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
like image 27
Raunak Thomas Avatar answered Sep 19 '22 01:09

Raunak Thomas


Here another way to open with out the vba

Start Excel Application > Go to File > Recent >

enter image description here

Hold Shift key and double click to open -

Doing this will prevent the Workbook_Open event from firing and the Auto_Open macro from running.

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


For VBA Work with Application.EnableEvents property (Excel)

like image 7
0m3r Avatar answered Sep 21 '22 01:09

0m3r