如何从 .net 代码检查 Excel 应用程序是否启用了“信任对 VBA 项目对象模型的访问”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5300770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check from .net code whether "Trust access to the VBA project object model" is enabled or not for an Excel application?
提问by Kushal Waikar
How to check from .net code whether "Trust access to the VBA project object model" is enabled or not for an Excel application?
如何从 .net 代码检查Excel 应用程序是否启用了“对 VBA 项目对象模型的信任访问”?
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
我可以手动从 Excel 应用程序检查它 -文件>选项>信任中心>信任中心设置>宏设置>信任对 VBA 项目对象模型的访问
回答by i_saw_drones
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):
简短的回答是您不能使用 Excel 对象模型(即通过 PIA)直接访问此设置。但是,您可以从以下位置的注册表中检查此设置(这里我假设您使用的是 Office 2007 - 12.0 版):
HKEY_CURRENT_USER\Software\Microsoft\Office.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.
这是一个 DWORD,它将是 0 或 1,具体取决于是否启用了“对 VBA 对象模型的信任访问”。
However, this setting can be overriden by another registry key located at:
但是,此设置可以被位于以下位置的另一个注册表项覆盖:
HKEY_LOCAL_MACHINE\Software\Microsoft\Office.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.
这又是一个 DWORD,但是,如果该值为 0,这意味着无论 HKCU 值设置为什么,都将拒绝访问 VBOM。如果 HKLM 中的值为 1 或缺失,则 HKCU 密钥将控制对 VBOM 的访问。
Therefore, all you need to do is to check these two keys via the Registry methods in .NET.
因此,您需要做的就是通过 .NET 中的 Registry 方法检查这两个键。
回答by Brad
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
回答by Pieter Geerkens
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 Modelis not trusted.
根据我的经验,只要VBA 项目对象模型不受信任,Application.VBE
就会(取决于 OFFICE 版本)要么是*null*
要么抛出一个。COMException
If one is using Office.Interopin an Add-In then Globals.ThisAddIn.Application.VBE
will perform the necessary test.
如果在加载项中使用Office.Interop,Globals.ThisAddIn.Application.VBE
则将执行必要的测试。
I have used this proxy for years successfully.
我已成功使用此代理多年。
回答by Zorro
Search the registry for all instances of "AccessVBOM" and change the Dword settings to a 1.
在注册表中搜索“AccessVBOM”的所有实例并将 Dword 设置更改为 1。
That should turn it on.
那应该打开它。