Yu Chen
Yu Chen

Reputation: 7440

Disabling macros from other Excel sheets

I'm working on a few VBA macros in Excel for a project. Is there a way to ensure that VBA macros don't launch from other Excel workbooks when you are opening them through a macro?

For example, let's say I have a macro inside one workbook to open some other Excel file:

Sheets("Sheet1").Select 
PathName = Range("D3").Value 
Filename = Range("D4").Value 
TabName = Range("D5").Value 
ControlFile = ActiveWorkbook.Name 
Workbooks.Open Filename:=PathName &; Filename 

What I've usually done is include a Application.EnableEvents = False to the subroutine to ensure that I'm not triggering code upon opening the other workbooks. This is also the methodology suggested by this prior SO post.

But two questions come to mind:

  1. Is this "secure"? Are there other ways that users could write their own macros that execute on my active application even if I disable events?

  2. Are there other workarounds besides fully disabling events? This seems somewhat limiting and almost "throwing the baby out with the bathwater".

Upvotes: 0

Views: 1570

Answers (1)

mooseman
mooseman

Reputation: 2017

This seems to open the file without the macros and it's read only. When testing this, I can't even see the macro module in VBA editor for the opened file..

Application.AutomationSecurity = msoAutomationSecurityForceDisable
Workbooks.Open Filename:=strFilepath & strFilename, ReadOnly:=True
Application.AutomationSecurity = msoAutomationSecurityByUI

Upvotes: 1

Related Questions