Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable macros when opening excel workbook from VBA

Status
Not open for further replies.

SunGodly

Programmer
Jul 16, 2002
40
US
Hi All,

I need to open a workbook from VBA without running the 'Workbook_Open' routine that is attached to it. I have seen a suggestion in this forum using a global variable but I can't seem to make it work...Any other suggestions?
 
all you need to do is hold down the shift key when selecting the file and the macros will be disabled.
Hope it helps.
 
I apologise for the global variable tip. I based it on a bug I had when I ran two spreadsheets side by side and the globals clashed. Now I can't get it to work.

You could use the application.username as a flag tho.

sub opener()
dim namehold as string
namehold = application.username
application.username = "No Macros"
'open the workbook
application.username = namehold
end sub

and in
sub workbook_open()
if application.username = "No Macros" then exit sub

If it crashes on the open event, you have lost the users username tho. There is probably an easier way but in the absence of that, this will work.

Other methods would be to get the opening files to check for the presence of the file that you use for opening and quit macros if it's there.
 
Okay, I found a VERY simple method that appears to work...

Application.EnableEvents = False

I just tried it and it appears to do the job very nicely. I do not know if the property needs to be reset to True or if it resets automatically.

Anyway, many thanks to everyone who replied, and I hope this is helpful to you as well.
 
I would suggest that you reset Application.EnableEvents as it will not automatically reset, and is application wide in it's effect.

A.C.
 
Following up on Acron's advice, I would also wrap the code to open the other workbook in some error trapping. If an untrapped error occurs, the subsequent
Code:
Application.EnableEvents = True
line will not execute. All event handling will then be deactivated.


Regards,
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top