适用于Office 2016 for Mac的VB宏在每次尝试访问文件时都需要权限!有没有办法绕过这种行为?

ram*_*ora 5 excel vba excel-vba mac-office

每次宏尝试访问文件时,Office 2016中的任何VB宏都会向用户显示一个对话框,要求获得权限!有没有办法避免它.

ram*_*ora 8

与Office for Mac 2011中的VB宏不同,Office 2016 for Mac中的VB宏默认情况下无权访问外部文件.Office 2016 for Mac应用程序是沙盒,因此缺少访问外部文件所需的权限.

如果应用程序尚无权访问,则会更改现有宏文件命令以提示用户进行文件访问.这意味着访问外部文件的宏无法无人值守运行; 他们将需要用户交互来在第一次引用每个文件时批准文件访问.

开发人员应使用GrantAccessToMultipleFiles命令(请参阅下一节)来避免此体验.此命令允许您的应用程序一次获得所有文件的权限,从而避免困难的用户体验.

GrantAccessToMultipleFiles
这使您可以输入文件路径数组并提示用户访问它们的权限.

Boolean  GrantAccessToMultipleFiles(fileArray) 
Run Code Online (Sandbox Code Playgroud)
  • 参数

    • fileArray - POSIX文件路径的数组.
  • 返回值

    • True - 用户授予文件权限.
    • False - 用户拒绝对文件的权限.


注意:授予后,权限将与应用程序一起存储,用户无需再授予该文件的权限.

例:   

Sub requestFileAccess()? 
? 
'Declare Variables? 
? ? Dim fileAccessGranted As Boolean? 
? ? Dim filePermissionCandidates 
? 
?'Create an array with file paths for which permissions are needed? 
? ? filePermissionCandidates = Array("/Users/<user>/Desktop/test1.txt", "/Users/<user>/Desktop/test2.txt") 
? 
'Request Access from User? 
? ? fileAccessGranted = GrantAccessToMultipleFiles(filePermissionCandidates) 'returns true if access granted, false otherwise? 
? ??? 
? 
End Sub
Run Code Online (Sandbox Code Playgroud)