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

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
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:

msoAutomationSecurityForceDisableDisables the macros in all documents that are opened by previously enabled macros. This setting provides the highest level of security.
msoAutomationSecurityByUIRespects the Warn before opening a file that contains macros preference for macros in all documents opened by previously enabled macros.
msoAutomationSecurityLowEnables the macros in all document that are opened by previously enabled macros.


<tbody>
</tbody>
 
Upvote 0

Forum statistics

Threads
1,214,817
Messages
6,121,720
Members
449,050
Latest member
MiguekHeka

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