Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在不打开文件的情况下,使用VBA检测Excel文件是否具有IRM(信息权限管理)限制_Excel_Vba_Ms Access - Fatal编程技术网

在不打开文件的情况下,使用VBA检测Excel文件是否具有IRM(信息权限管理)限制

在不打开文件的情况下,使用VBA检测Excel文件是否具有IRM(信息权限管理)限制,excel,vba,ms-access,Excel,Vba,Ms Access,我有一个使用VBA用Microsoft Access编写的应用程序,它可以打开Excel文件,以各种方式处理内容,然后关闭它们。如果文件无法打开,那么我需要检测并跳过该文件,否则应用程序将有效冻结 Excel文件来源众多,如果它们受到限制,我没有打开它们的帐户凭据 使用受密码保护的文件,我可以提供不正确的密码,检测错误,然后跳过该文件 Application.workbook.Open(文件名,False,,“xxxx”,True) 如果Excel文件已应用IRM(信息权限管理)限制,则在Exc

我有一个使用VBA用Microsoft Access编写的应用程序,它可以打开Excel文件,以各种方式处理内容,然后关闭它们。如果文件无法打开,那么我需要检测并跳过该文件,否则应用程序将有效冻结

Excel文件来源众多,如果它们受到限制,我没有打开它们的帐户凭据

使用受密码保护的文件,我可以提供不正确的密码,检测错误,然后跳过该文件

Application.workbook.Open(文件名,False,,“xxxx”,True)

如果Excel文件已应用IRM(信息权限管理)限制,则在Excel应用程序中打开该文件时,系统会提示您使用具有打开该文件权限的帐户登录Excel

如果在Excel应用程序不可见的情况下尝试使用上面的VBA代码打开文件,则该过程将停止,不会生成错误

我需要做的是,在尝试打开该文件之前检测该文件是否已应用IRM,或者尝试打开该文件并生成我可以检测到的错误


提前感谢您对解决此问题的帮助。

文档中有一个明确的示例:

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

来源:

在尝试了许多方法之后,我发现受限制的文件包含一个字符串,表示它们受权限管理的保护

因此,如果您打开该文件并检查其内容,您可以检测其是否受到限制,而无需尝试将其加载到Excel中

此函数适用于我使用过的所有文件

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

我见过这个。问题是,它需要ActiveWorkbook来获取权限,并且只有在可以打开该文件的情况下,才能将其作为ActiveWorkbook获取。如果您没有权限,则无法打开它,在尝试打开它之前,我需要查看是否设置了任何权限。感谢您花时间查看此文件。@jhTuppeny根据定义,如果您没有读取该文件的权限,那么您就没有读取权限。我不需要读取权限,我只需要知道是否设置了权限,这样我就可以跳过该文件而不必尝试打开它。如果在没有权限的情况下尝试打开它,则会生成错误。这是一项基于web的功能(O365),因此可能没有VBA(COM)工具来查询已关闭文档的保护设置。您可能需要使用新的office加载项工具来完成您想要的任务: