Most Valuable Professional


View Jan Karel Pieterse's profile on LinkedIn subscribe to rss feed
Subscribe in a reader

Home > English site > Articles > Prevent Open Event
Deze pagina in het Nederlands

Preventing auto_open and Workbook_Open events from running

Introduction

As a fulltime developer I oftentimes open files containing VBA and want to be able to run code. At the same time I sometimes want to prevent Workbook_Open event code or an Auto_Open macro from running. This little article shows you how to achieve that.

Why would you want to do this? Typically, I use this on my own files in which I am still developing. The Open event might contain code that takes a while to run, or configures the project in a way which I don't want to happen when I start working with the file.

If the file you opened using the methods described below contains other event handlers, note that these event handlers will remain disabled until you start a macro manually (by clicking a command button or a menu entry) or if a User Defined function in your code has been called.

I do not recommend using this method to open files from sources you do not know or trust. The VBA code can contain events which will eventually run and possibly cause trouble.

Excel 2010

Macro security set to low or trusted document

If you have set your macro security to "Enable All Macros" or you have already set the document to trusted or the document is in a trusted folder, click File, Open, select your file and hold down the shift key when you click the Open button:

Selecting the file from the File, Open dialog

Fig. 1: Selecting the file from the File, Open dialog (Excel 2010)

Of course your file might be listed in the Most Recently Used files (MRU) list.  In that case, hold shift when you click the file in the list:

Clicking the file on the MRU

Fig. 2: Clicking the file on the MRU (Excel 2010)

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

Macro security set to prompt

If your document is not in a trusted folder, has never been set to be trusted and macro security is set to something other than enable all macros to run, you can repeat what I've shown above.

Normally, if you open such a document, Excel will show the security bar. In this situation however, due to you holding down shift, Excel shows this window:

Enable macros dialog

fig 3: Enable macros dialog (Excel 2010)

Because you held down the shift button when you clicked the file in the MRU or when you clicked the Open button, you can now just click "Enable macros" and no Automacros will run.

Excel 2007 and up

Macro security set to low or trusted document

Note that this seems to fail if the trusted folder is on a network share!

If you have set your macro security to "Enable All Macros" or you have already set the document to trusted or the document is in a trusted folder, click File, Open, select your file and hold down the shift key when you click the Open button:

Selecting the file from the File, Open dialog

Fig. 4: Selecting the file from the File, Open dialog (Excel 2007 and up)

Of course your file might be listed in the Most Recently Used files (MRU) list.  In that case, hold shift when you click the file in the list:

Clicking the file on the MRU

Fig. 2: Clicking the file on the MRU (Excel 2007 and  up)

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

Macro security set to prompt

If your document is not in a trusted folder, has never been set to be trusted and macro security is set to something other than enable all macros to run, you can repeat what I've shown above.

Normally, if you open such a document, Excel will show the security bar. In this situation however, due to you holding down shift, Excel shows this window:

Enable macros dialog

fig 3: Enable macros dialog (Excel 2007 and up)

Because you held down the shift button when you clicked the file in the MRU or when you clicked the Open button, you can now just click "Enable macros" and no Automacros will run. 

Excel 97, 2000, XP, 2003

Macro security set to low or trusted document

In case your macro security is set to low, or your VBA code is signed and you have set the publisher as trusted, you must hold down the shift key when you click the Open button on the File, Open dialog:

Opening the file from the File, Open dialog

Fig. 7: Opening the file from the File, Open dialog (Excel 97-2003)

Of course your file might be listed in the Most Recently Used files (MRU) list.  In that case, hold shift when you click the file in the list:

Clicking the file on the MRU

Fig. 8: Clicking the file on the MRU (Excel 97-2003)

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

Macro security set to medium or higher

If your macro security is set to al least "Medium" and you have never set it's publisher to trusted (for a signed macro) you can forget about holding shift when opening the file or clicking the file on the MRU list. Instead, you must hold shift when you enable macros:

Hold shift when you press Enable macros

Fig.9: Hold shift when you press Enable macros

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

 


Comments

Showing last 8 comments of 49 in total (Show All Comments):

 


Comment by: Jan Karel Pieterse (7/5/2013 11:58:03 AM)

Hi Chris,

The % shortcut should lead you to here:

C:\Users\[yourusername]\AppData\Roaming\Microsoft\Excel\XLSTART

 


Comment by: Chris (7/10/2013 2:26:41 PM)

Thanks Jan! I had to do it on my work computer cause that's where I was having the issue. I deleted those file from the start-up folder and now it loads up just fine.

 


Comment by: Anthony Nashokigwa (12/18/2013 11:12:37 AM)

I have a windows task scheduler that opens my spreadsheet once a day. I have procedures embedded in Public Sub Workbook_Open of the spreadsheet which obviously run automatically when file is opened. The problem I'm having is that I would like Workbook_Open to be disabled when I personally open the file i.e. double clicking, to prevent the procedures from automatically running.

Any thoughts?

 


Comment by: Jan Karel Pieterse (12/18/2013 11:51:20 AM)

Hi Anthony,

Doesn't the suggested method on this page work?

 


Comment by: Anthony Nashokigwa (12/18/2013 12:42:10 PM)

Hi Jan - it does work, but I am trying to avoid as much manual intervention as possible, as multiple people will are using this file and at some point or another, someone is going to forget to hold down the SHIT key.Perhaps what I could do is write a macro that simulates holding down the the shift button whilst opening the file?

Your thoughts?

 


Comment by: Jan Karel Pieterse (12/18/2013 3:08:25 PM)

Hi Anthony,

I guess you should be asking yourself a question like "under which circumstances must the Workbook_Open event be ignored" and then act accordingly.

For example, if the scheduler is run from a machine with a specific user logged in, you could check for the environ("username") property value and decide whether or not to execute the content of Workbook_Open.

 


Comment by: Scott (4/7/2014 8:00:58 PM)

I have a macro that opens all the other workbooks in a folder that have macros to get some information about those files. I do not want the auto_open macros to run. However, they are running when the vb code opens them.

Supposedly, workbooks with auto_open macros will not automatically run if opened with vb code. I find this not to be true. They are running and I don't want them to. I wish to find a way to prevent this.

 


Comment by: Jan Karel Pieterse (4/9/2014 7:22:59 AM)

Hi Scott,

You could turn off macros of those files entirely:

Application.AutomationSecurity=msoAutomationSecurityForceDisable

 


Have a question, comment or suggestion? Then please use this form.

If your question is not directly related to this web page, but rather a more general "How do I do this" Excel question, then I advise you to ask your question here: www.eileenslounge.com.

Please enter your name (required):

Your e-mail address (optional but if you want me to respond it helps!; will not be shown, nor be used to send you unsolicited information):

Your request or comment:

To post VBA code in your comment, use [VB] tags, like this: [VB]Code goes here[/VB].