3

System Details.

  • Windows 10
  • Excel 365 64-bit V16.0 (Apps for Enterprise)

Issue.

I have a macro that opens a file, checks if the user has allowed programmatic access to the VBA Project in the trust center and raises an alert if they have not. This works fine on windows 7 machines with both 32bit and 64bit Excel from 2013 onwards.

The specific problem I am having on the Windows 10 machine is that when workbooks.open(path) is executed the file opens but in the VBA Project explorer window the VBAProject object for that file does not appear. I also get a popup saying that code cannot be run in break mode when I try this while stepping through the code. When run on Win 7 with this version of Excel and others both 32 and 64-bit the VBAProject object appears and no popup is generated.

This lack of the VBA Project causes an issue later when I run set vbproj = ActiveWorkbook.VBProject. At this point when stepping through the code execution halts with no messages at all. The Project pops up in the Project explorer window and it takes me to a module in the opened workbook. This gets opened with Design Mode turned on and when you click to turn it off I get an error message saying 'Macros have been disabled'

Notes

  • set vbproj = ActiveWorkbook.VBProject works fine if the file is open and the VBAProject is visible in the Project explorer window.
  • If the file being opened does not contain a macro then it works correctly.
  • I have made sure that the files are in a Trusted Location.
  • I have set Enable all macros in the Macro Settings of the Trust Center.
  • When opening the files manually I do not get any alerts that macros have been disabled by an administrator and I do not get any message asking if I want to enable macros.
  • I have looked at W10 group policies to see if there is one that would block VBAProjects / Macros from files opened via VBA and there does not appear to be one as far as I can see.
  • The Trust Center setting for programmatic access to the VBA Project does not matter to this test case. It will return True if it is allowed and False if it is not allowed.
  • This has been tested on another Windows 10 laptop with the same version of Excel 64-bit and it has the same result so is not an issue with a specific users laptop.
  • I removed the folder from the Trusted Location on the Windows 7 machine and the code still executes correctly and returns True/False.
  • In the actual macro this check is done for each opened file in a loop and the variant vbproj is used to remove data from the modules within that project. I have considered working around the issue by using Application.onTime and that may be a solution but have not spent much time on that and with how it halts it may not be viable.

Attempted Solutions

  • I have tried set wb = workbooks.open(path) to open the workbook and then using set vbproj = wb.VBProject but it has the same behavior as above.
  • I have tried doing ActiveWorkbook.Activate and similar actions to see if that will make the VBA project appear, it does not.
  • I have tried setting Application.FileValidation = msoFileValidationSkip before opening the file, this does not change the behavior.
  • I have tried setting Application.EnableEvents=False before opening the file, this does not change the behavior.
  • I have tried making vbproj a variant, an object and a VBProject, this does not change the behavior.

Steps to Recreate

  • Create a new workbook.
  • Put the below code into Module 1.
Function projectAccess()

    Dim vbproj As Variant
    
    On Error GoTo noaccess
    Set vbproj = ActiveWorkbook.VBProject 'If access is denied an error is raised.
    
    projectAccess = True
    
    Exit Function
    
noaccess:
    projectAccess = False
    
End Function

Sub openfile()

    Dim filepath As String
    
    filepath = Application.ThisWorkbook.Path
    Workbooks.Open (filepath & "\openfile.xlsm")

    Debug.Print projectAccess

End Sub
  • Save the workbook
  • Create a 2nd workbook, in my case it was called openfile.xlsm and put some code into Module 1.
  • put both workbooks in the same location and make sure it is a Trusted Location in Excel.
  • run openfile().
  • If successful the immediate window will display True / False depending on the Trust Center setting.

I am out of ideas. Any suggestions for some setting that I may have overlooked to make the Win 10 machines behave the same as the Win 7 machines or suggestions for a possible work around?

Even if it is a group policy setting that would be something I can raise with IT as long as I know what to ask for.

Many Thanks

Andrew

EDIT: Thanks to Rory in the comments the issue was making sure that the automation security was set like so Application.AutomationSecurity = msoAutomationSecurityLow as the way the new Win 10 + Office 64 systems have been set up by my IT dept is to have it default to msoAutomationForceDisable

Andrew
  • 31
  • 2
  • 2
    What does `?Application.AutomationSecurity` return in the Immediate Window? – Rory Dec 15 '20 at 16:15
  • @Rory On the win 7 machines where it works I get a 1. On the Win 10 machines I get 3. So having a guess that setting that to 1 which I assume is low will get it working. thank you. Can't believe I missed it. – Andrew Dec 15 '20 at 16:38
  • 1
    Yes - 3 is `ForceDisable` – Rory Dec 15 '20 at 16:46
  • @Rory Just tested by setting application.AutomationSecurity = msoAutomationSecurityLow and while I got the 'cannot run in break mode' box it happily loaded the file and accessed the VBProject object. Thanks again. – Andrew Dec 15 '20 at 16:48
  • I had similar issue before where Workbook_Open codes says cannot run in break mode, how I got it "fixed" is to open that .xlsm file on one that works, then in VBE, **Clear All Breakpoints** and Sign then save the file. Then open in the affected computer is a joy! – PatricK Dec 16 '20 at 04:36

0 Answers0