Skip 'Enable Macros' Dialogue box when Opening a File with VBA

Caleeco

Well-known Member
Joined
Jan 9, 2016
Messages
980
Office Version
  1. 2010
Platform
  1. Windows
Hello,

I am trying to import data from a workbook that is closed into my currently open workbook. I found some code online which I chopped up, However, I am having problems with the statement below:

Code:
    Application.EnableEvents = False
    Workbooks.Open FileName:=strFilepath & strFilename, ReadOnly:=True
    Application.EnableEvents = True

I still get the prompt to enable or disable macros. I want to be able to diable them and open the file as read-only. Then copy out data from the opened workbook.

Code:
Sub UpdateSRelData()


'------Variables-------
Dim strFilepath As String 'Filepath for the file that needs opening
Dim strFilename As String 'Filename
Dim wbKPI As Workbook 'Wb that is currently open
Dim wbSRR As Workbook 'wb to oen
strFilepath = "S:\<some path="" directory="">\"
strFilename = "S-Release Requests Sheet.xlsm"

'--------- Check if S-Release Requests Spreadsheet is already open
Dim Ret
Ret = IsWorkBookOpen(strFilepath & strFilename)

If Ret = True Then
    MsgBox "The Excel File " & strFilename & " is already opened by yourself or another user. Please close it and re-run this macro to obtain the latest data." & vbLf & "This Macro will now run on the read-only version of the available file.", vbCritical, "Error"
    'Exit Sub
End If

If Dir(strFilepath & strFilename) = "" Then
    MsgBox "The File named " & strFilename & " does not exist." & vbLf & "The P Parts List has not been updated.", vbCritical, "Error"
    Exit Sub
Else
    Application.EnableEvents = False
    Workbooks.Open FileName:=strFilepath & strFilename, ReadOnly:=True
    Application.EnableEvents = True
End If

Set wbSRR = Workbooks(strFilename)
Set wbKPI = Workbooks("Offload's Request for Material Summary KPIs.xlsm")
 
End Sub

Function IsWorkBookOpen(FileName As String)
    Dim ff As Long, ErrNo As Long
    On Error Resume Next
    ff = FreeFile()
    Open FileName For Input Lock Read As #ff
    Close ff
    ErrNo = Err
    On Error GoTo 0

    Select Case ErrNo
    Case 0:    IsWorkBookOpen = False
    Case 70:   IsWorkBookOpen = True
    Case Else: Error ErrNo
    End Select
End Function

Thanks in advance!
Calecco</some>
 
Last edited:

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Try this:

Code:
    Application.AutomationSecurity = msoAutomationSecurityForceDisable
    Workbooks.Open Filename:=strFilepath & strFilename, ReadOnly:=True
    Application.AutomationSecurity = msoAutomationSecurityByUI
 
Upvote 0
Try this:

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

Hello Rory,

Thanks for the lightning quick response! Yes, that does just what I want :)

Out of interest, can I enable macros with this statement?

Code:
    Application.AutomationSecurity = msoAutomationSecurityForceEnable
    Workbooks.Open FileName:=strFilepath & strFilename, ReadOnly:=True
    Application.AutomationSecurity = msoAutomationSecurityByUI
 
Upvote 0
Ah i think I found it, the microsoft support website states the following:

[TABLE="class: sbody-table table"]

<tbody>[TR="class: sbody-tr"]
[TD="class: sbody-td"]msoAutomationSecurityForceDisable[/TD]
[TD="class: sbody-td"]Disables the macros in all documents that are opened by previously enabled macros. This setting provides the highest level of security.[/TD]
[/TR]
[TR="class: sbody-tr"]
[TD="class: sbody-td"]msoAutomationSecurityByUI[/TD]
[TD="class: sbody-td"]Respects the Warn before opening a file that contains macros preference for macros in all documents opened by previously enabled macros.[/TD]
[/TR]
[TR="class: sbody-tr"]
[TD="class: sbody-td"]msoAutomationSecurityLow[/TD]
[TD="class: sbody-td"]Enables the macros in all document that are opened by previously enabled macros.[/TD]
[/TR]
</tbody>
[/TABLE]
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top