Excel 365 VBA判断工作簿永久信任状态及跳转信任设置入口问询
问题1:VBA判断Excel 365工作簿是否为永久信任状态
- 结论:可以实现,可通过读取Windows注册表中Office的信任文档记录区分永久信任与临时信任
- 原理:用户点击「信任此文档」生成的永久信任记录会持久化写入注册表路径
HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Excel\Security\Trusted Documents\TrustRecords下,键名为当前工作簿全路径的二进制转码值;仅本次会话启用宏的临时信任不会写入该注册表,仅保存在当前Excel进程内存中 - 参考代码:
Function IsPermanentlyTrusted() As Boolean Dim wbFullPath As String Dim regPath As String Dim regObj As Object Dim allKeys As Variant Dim i As Integer wbFullPath = ThisWorkbook.FullName regPath = "HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Excel\Security\Trusted Documents\TrustRecords\" Set regObj = CreateObject("WScript.Shell") On Error Resume Next allKeys = regObj.RegRead(regPath) On Error GoTo 0 If IsEmpty(allKeys) Then IsPermanentlyTrusted = False Exit Function End If For i = LBound(allKeys) To UBound(allKeys) ' 注册表键值解码后匹配工作簿全路径 If InStr(1, StrConv(allKeys(i), vbUnicode), wbFullPath, vbTextCompare) > 0 Then IsPermanentlyTrusted = True Exit Function End If Next i End Function
注意:32位和64位Office注册表路径无差异,该代码同时兼容两种版本
问题2:无SendKeys直接跳转至「信任此文档」设置页
- 结论:可通过Office内置的命令栏调用方法直接跳转,无需SendKeys模拟按键
- 实现方法:调用
Application.CommandBars.ExecuteMso方法即可直接打开对应页面,参考代码:
Sub GoToTrustDocumentPage() ' 直接跳转到Excel「文件」选项卡的「信息」页面,「信任此文档」按钮位于该页面顶部安全提示区域 Application.CommandBars.ExecuteMso "FileTabInfo" End Sub
- 补充:如果需要直接唤起信任中心的文档信任设置弹窗,可将上述方法的参数替换为
TrustCenterDocumentTrustSettings
内容的提问来源于stack exchange,提问作者Daniel Barrett
相关产品推荐
相关产品推荐

