如何从 .net 代码检查“信任对 VBA 项目对象模型的访问”是否有效对于 Excel 应用程序是否启用?

发布于 2024-10-22 04:05:39 字数 264 浏览 3 评论 0原文

如何从 .net 代码检查 Excel 应用程序是否启用了“信任对 VBA 项目对象模型的访问”?

我可以从 Excel 应用程序手动检查它 - 文件>选项>信任中心>信任中心设置>宏设置>信任对 VBA 项目对象模型的访问

在此处输入图像描述

How to check from .net code whether "Trust access to the VBA project object model" is enabled or not for an Excel application?

Manually I can check it from Excel application- File>Options>Trust Center>Trust Center Settings>Macro Settings>Trust access to the VBA project object model

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

追风人 2024-10-29 04:05:39

简而言之,您无法使用 Excel 对象模型(即通过 PIA)直接访问此设置。
但是,您可以从以下位置的注册表中检查此设置(此处我假设您使用的是 Office 2007 - 版本 12.0):

HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Security\AccessVBOM

这是一个 DWORD,将为 0 或 1,具体取决于“信任访问VBA 对象模型”已启用。

但是,此设置可以被位于以下位置的另一个注册表项覆盖:

HKEY_LOCAL_MACHINE\Software\Microsoft\Office\12.0\Excel\Security\AccessVBOM

这又是一个 DWORD,但是,如果此值为 0,则意味着无论 HKCU 值设置为什么,对 VBOM 的访问都将被拒绝。如果 HKLM 中的值为 1 或缺失,则 HKCU 密钥将控制对 VBOM 的访问。

因此,您所需要做的就是通过.NET 中的注册表方法检查这两个键。

The short answer is that you cannot directly access this setting using the Excel object model (i.e. through PIAs).
However, instead, you can check this setting from the registry in the following location (here I assume that you're using Office 2007 - version 12.0):

HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Security\AccessVBOM

this is a DWORD that will be 0 or 1 depending on whether the "Trust access to VBA Object Model" is enabled.

However, this setting can be overriden by another registry key located at:

HKEY_LOCAL_MACHINE\Software\Microsoft\Office\12.0\Excel\Security\AccessVBOM

this is again a DWORD, however, if this value is 0 this means that no matter what the HKCU value is set to, then access to the VBOM will be denied. If the value in HKLM is 1 or missing, then the HKCU key will control the access to the VBOM.

Therefore, all you need to do is to check these two keys via the Registry methods in .NET.

辞别 2024-10-29 04:05:39

这对我有用

Function VBATrusted() As Boolean
    On Error Resume Next
    VBATrusted = (Application.VBE.VBProjects.Count) > 0
End Function

Private Sub Workbook_Open()
    If Not VBATrusted() Then
    MsgBox "No Access to VB Project" & vbLf & _
      "Please allow access in Trusted Sources" & vbLf & _
      "File > Options > Trust Center > Trust Center Settings > Macro Settings > Trust Access..."
    End If
End Sub

来源 https://www.mrexcel.com/forum/excel-questions/659774-checking-if-trust-access-visual-basic-project-ticked.html

This worked for me

Function VBATrusted() As Boolean
    On Error Resume Next
    VBATrusted = (Application.VBE.VBProjects.Count) > 0
End Function

Private Sub Workbook_Open()
    If Not VBATrusted() Then
    MsgBox "No Access to VB Project" & vbLf & _
      "Please allow access in Trusted Sources" & vbLf & _
      "File > Options > Trust Center > Trust Center Settings > Macro Settings > Trust Access..."
    End If
End Sub

Source https://www.mrexcel.com/forum/excel-questions/659774-checking-if-trust-access-visual-basic-project-ticked.html

风月客 2024-10-29 04:05:39

在注册表中搜索“AccessVBOM”的所有实例,并将 Dword 设置更改为 1。

这应该将其打开。

Search the registry for all instances of "AccessVBOM" and change the Dword settings to a 1.

That should turn it on.

半透明的墙 2024-10-29 04:05:39

根据我的经验,只要 Application.VBE 将(取决于 OFFICE 版本)为 *null* 或抛出 COMException >VBA 项目对象模型不受信任。

如果在加载项中使用 Office.Interop,则 Globals.ThisAddIn.Application.VBE 将执行必要的测试。

我已经成功使用这个代理多年了。

It has been my experience that Application.VBE will (depending on OFFICE version) either be *null* or throw a COMException whenever the VBA Project Object Model is not trusted.

If one is using Office.Interop in an Add-In then Globals.ThisAddIn.Application.VBE will perform the necessary test.

I have used this proxy for years successfully.

瑕疵 2024-10-29 04:05:39

此代码检查 VBA 对象模型是否可信。如果不是,代码将打开“宏设置”对话框,这样用户就不必导航到它。

'_2023_03_20
Function fTestGetAccessVBE()
    MsgBox fGetAccessVBE(True)
End Function

'_2024_02_15
Function fGetAccessVBE(Optional blnDialog As Boolean = False) As Boolean
Dim blnAccess As Boolean

    On Error Resume Next    '_2024_02_15
    blnAccess = Not IsError(Application.VBE.CommandBars.Count > 0)  'error if access not trusted

    Select Case True
    Case blnAccess = True   'we have access
    Case blnDialog = True   'else if we want to prompt user to enable
        MsgBox "Programmatic access to the VBA project is not trusted." 'prompt
        Application.CommandBars.ExecuteMso ("MacroSecurity")    'open macro setting dialog
        blnAccess = Not IsError(Application.VBE.CommandBars.Count > 0)  'now check again
    End Select

    fGetAccessVBE = blnAccess   'return access state
End Function

'_2021_10_01
Sub fOpenVbaEditor()
    If fGetAccessVBE(True) = False Then Exit Sub    'done if no access
    Application.VBE.MainWindow.Visible = True   'else open vba
End Sub

This code checks if the VBA object model is trusted. If it is not, the code opens the Macro Setting dialog so the user doesn't have to navigate to it.

'_2023_03_20
Function fTestGetAccessVBE()
    MsgBox fGetAccessVBE(True)
End Function

'_2024_02_15
Function fGetAccessVBE(Optional blnDialog As Boolean = False) As Boolean
Dim blnAccess As Boolean

    On Error Resume Next    '_2024_02_15
    blnAccess = Not IsError(Application.VBE.CommandBars.Count > 0)  'error if access not trusted

    Select Case True
    Case blnAccess = True   'we have access
    Case blnDialog = True   'else if we want to prompt user to enable
        MsgBox "Programmatic access to the VBA project is not trusted." 'prompt
        Application.CommandBars.ExecuteMso ("MacroSecurity")    'open macro setting dialog
        blnAccess = Not IsError(Application.VBE.CommandBars.Count > 0)  'now check again
    End Select

    fGetAccessVBE = blnAccess   'return access state
End Function

'_2021_10_01
Sub fOpenVbaEditor()
    If fGetAccessVBE(True) = False Then Exit Sub    'done if no access
    Application.VBE.MainWindow.Visible = True   'else open vba
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文