Question: I have an application written in Microsoft Access using VBA which opens Excel files, processes the contents in various ways and then closes them. If the a file cannot be opened, then I need to detect this and skip the file otherwise the application effectively freezes.

The Excel files come from numerous sources and if they are restricted I don't have the account credentials to open them.

With a password protected file I can supply an incorrect password, detect the error and then skip the file.

`Application.Workbooks.Open(FileName, False, , , "xxxx", , True)`

If the Excel file has had IRM (Information Rights Management) Restrictions applied to it, then when you open the file in the Excel application you are prompted to sign into Excel with an account that has permission to open the file.

[![Account Sign In Prompt for IRM File](https://i.stack.imgur.com/O9To7.jpg)](https://i.stack.imgur.com/O9To7.jpg)

If you try to open the file using the VBA code above with the Excel application not visible, then the process just halts and no error is generated.

What I need to do is either detect that the file has IRM applied to it before trying to open it or try to open it and generate an error that I can detect.

Thanks in advance for any help in solving this.

Answer: 
After trying a number of approaches to this I found that restricted files contain a string that indicates that they are secured with rights management.

So if you open the file and examine the contents you can detect if it's restricted without trying to load it into Excel.

This function works with all the files that I have tried it with.

```
Function IsFileRestriced(FileName As String) As Boolean

   Dim lngFile As Long, lngPos As Long
   Dim strContent As String

   'Open the file in binary mode
   lngFile = FreeFile
   Open FileName For Binary As lngFile

   'Read the whole file into a string
   strContent = Space$(LOF(lngFile))
   Get #lngFile, , strContent

   'Check if the file has the rights management string
   lngPos = InStr(1, strContent, "Microsoft Rights Label")

   'Close the file
   Close #lngFile

   'Return the result
   If lngPos > 0 Then IsFileRestriced = True

End Function

```
There is a clear example in the documentation:

```
Sub ShowPermissions()
    Dim irmPermission As Office.Permission
    Dim strIRMInfo As String
    Set irmPermission = ActiveWorkbook.Permission
    If irmPermission.Enabled Then
        strIRMInfo = "Permissions are restricted on this document." & vbCrLf
        strIRMInfo = strIRMInfo & " View in trusted browser: " & _
        irmPermission.EnableTrustedBrowser & vbCrLf & _
        " Document author: " & irmPermission.DocumentAuthor & vbCrLf & _
        " Users with permissions: " & irmPermission.Count & vbCrLf & _
        " Cache licenses: " & irmPermission.StoreLicenses & vbCrLf & _
        " Request permission URL: " & irmPermission.RequestPermissionURL & vbCrLf
        If irmPermission.PermissionFromPolicy Then
            strIRMInfo = strIRMInfo & " Permissions applied from policy:" & vbCrLf & _
            " Policy name: " & irmPermission.PolicyName & vbCrLf & _
            " Policy description: " & irmPermission.PolicyDescription
        Else
            strIRMInfo = strIRMInfo & " Default permissions applied." & vbCrLf & _
            " Default policy name: " & irmPermission.PolicyName & vbCrLf & _
            " Default policy description: " & irmPermission.PolicyDescription
        End If
    Else
        strIRMInfo = "Permissions are NOT restricted on this document."
    End If

    MsgBox strIRMInfo, vbInformation + vbOKOnly, "IRM Information"
    Set irmPermission = Nothing

End Sub

```

SOURCE: <https://learn.microsoft.com/en-us/office/vba/api/office.permission>