Results 1 to 6 of 6
  1. #1
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Macro Security (2003)

    I don't often get involved in Excel projects, so I suspect I'm going to be asking a few questions in here as I get up to speed...
    <UL><LI>My Macro security is set to High, so I can only run signed Macros from trusted sources.
    <LI>I have some VBA code in an Excel workbook that prompts the user for another Excel file and then opens it.
    <LI>The Workbook I need to open has a number of events in it's This_Workbook module (Workbook_Open, Workbook_BeforeClose, Workbook_WindowActivate). These would cause Excel to display a custom toolbar when the workbook is active if I allowed them to run.
    <LI>If I open this workbook directly from the user interface then I see the dialog that says Macros are disabled, but everything functions normally
    <LI>If I open this workbook using <code>Workbooks.Open(filename)</code> then it appears to run some of the Macros, even though they are not signed and my Macro security is set to high. I'm not sure exactly which get run, but the end result is that the Workbook_BeforeClose routine generates an error trying to delete a toolbar that was never created[/list]Any suggestions on how to open this Workbook without it running any of its Macros?

    StuartR

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro Security (2003)

    You can turn off event handling temporarily:

    Application.EnableEvents = False
    Workbooks.Open "Book1.xls"
    Application.EnableEvents = True

  3. #3
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Macro Security (2003)

    In addition to Hans' answer, you can also use something like:
    <pre>Sub Security()
    Dim secAutomation As MsoAutomationSecurity
    secAutomation = Application.AutomationSecurity
    Application.AutomationSecurity = msoAutomationSecurityForceDisable
    Workbooks.Open strFilePath
    Application.AutomationSecurity = secAutomation
    End Sub
    </pre>

    HTH
    Regards,
    Rory

    Microsoft MVP - Excel

  4. #4
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Macro Security (2003)

    Rory,

    I have made a lot of progress and I have solved my problem in a different way - it turns out that the code was a hangover from a previous document management system and I was allowed to just delete the lot!

    I do still have a problem with my understanding though.

    Why did the code in this workbook run, even though my Macro security was set to high? This didn't happen if I opened the workbook from the user interface, but only if I opened it from within my macro. Since the user had selected the file from a dialog box, this could have been any file, and I thought setting Macro security to high would prevent unsigned code from running.

    StuartR

  5. #5
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Macro Security (2003)

    Stuart,
    When you open a workbook through code, the setting used is the Automation Security one, which is Low by default. (the theory being presumably that you have already allowed code to run in the calling workbook, so you clearly trust it - obviously, that's not always a good theory!) If I recall correctly, Auto_... macros do not fire but Workbook event macros do unless you disable events.
    Regards,
    Rory
    Regards,
    Rory

    Microsoft MVP - Excel

  6. #6
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Macro Security (2003)

    Thanks, that makes things much clearer.

    I still don't understand why this particular set of Macros generates an error trying to delete a non-existant toolbar in Workbook_BeforeClose when I open it this way, but not when it is opened from the UI - but I think it's time to move on.

    StuartR

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •