vba 以编程方式调出 Excel 的“信任中心设置”对话框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25234422/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:10:36  来源:igfitidea点击:

Programatically bringing up Excel's "Trust Center Settings" dialog

excelvbaexcel-vbavsto

提问by jeffreyweir

I need users to click the "Trust access to the VBA project object model" so that an app I'm building can help them import code to the VBE.

我需要用户单击“信任对 VBA 项目对象模型的访问”,以便我正在构建的应用程序可以帮助他们将代码导入 VBE。

I could display a messagebox or provide documentation telling them how to change setting (and warning about repercussions if they do). But that setting is still buried 5 clicks down in the Excel UI...in fact even I have trouble remembering where to find it.

我可以显示一个消息框或提供文档告诉他们如何更改设置(并警告他们如果这样做会产生影响)。但是该设置仍然隐藏在 Excel 用户界面中 5 次点击......事实上,即使我也很难记住在哪里找到它。

So what I'd like to do is programmatically open that window for them.

所以我想做的是以编程方式为他们打开那个窗口。

I could probably use the notoriously fickle SendKeys method, but I wonder if there is a better way...

我可能可以使用臭名昭著的善变 SendKeys 方法,但我想知道是否有更好的方法......

(I see that you can actually setaccess to the VBA project object model for users via a macro by using a VBS script to change the key's value when the target Office application is not running them as per this MSDN articlebut I would much prefer that the user manually enables this option or elects not to).

(我看到,当目标 Office 应用程序未按照此 MSDN 文章运行它们时,您可以通过使用 VBS 脚本更改密钥值,从而通过宏为用户设置对 VBA 项目对象模型的访问权限,但我更喜欢用户手动启用此选项或选择不启用)。

Edit: VBA orVSTO solutions are fine, or anything else you can think of.

编辑:VBAVSTO 解决方案都很好,或者您能想到的任何其他解决方案。

回答by David Zemens

This will bring up the dialog.

这将打开对话框。

Application.CommandBars.ExecuteMso("MacroSecurity")

Alternatively, this does the same thing:

或者,这也做同样的事情:

Application.CommandBars.FindControl(Id:=3627).Execute

The user will need to check the box, it cannot be checked programmatically even using SendKeys.

用户需要选中该框,即使使用 SendKeys 也无法以编程方式选中它。

Extra Credit: Is it possible to change the registry key?

额外信用:是否可以更改注册表项?

There is also a registry key which you may be able to change using VBA.

还有一个注册表项,您可以使用 VBA 更改它。

enter image description here

在此处输入图片说明

I thought you might be able to use a subroutine like this to set the registry key to "allow access". However, as I tested this and it doeschange the registry key value, it doesn't seem to have any effect on my ability to access the VBOM:

我认为您可以使用这样的子例程将注册表项设置为“允许访问”。但是,当我对此进行测试并且它确实更改了注册表项值时,它似乎对我访问 VBOM 的能力没有任何影响:

  • If my settings do not allow access, and I change the key value to 1, then I get a 1004 error.
  • If my settings doallow access, and I change the key value to 0, sample code that manipulates the VBOM still works.
  • If I change the registry key programmatically, it reverts to its previous state when re-starting the Excel Application.
  • 如果我的设置不允许访问,并且我将键值更改为 1,那么我会收到 1004 错误。
  • 如果我的设置允许访问,和我的键值改为0,示例代码,操纵VBOM仍然有效。
  • 如果我以编程方式更改注册表项,它会在重新启动 Excel 应用程序时恢复到以前的状态。

It's possible I did something wrong, so I will leave this here on the off-chance that someone else can get it to work. I have used this sort of function to set custom registry keys for my own applications, i.e., to store the application's current version #, etc. but perhaps this part of the registry is simply locked down and can't be manipulated this way.

我可能做错了什么,所以我会把它留在这里,以防其他人可以让它工作。我已经使用这种功能为我自己的应用程序设置自定义注册表项,即存储应用程序的当前版本 # 等,但也许注册表的这部分只是被锁定,无法通过这种方式进行操作。

Const regKey As String = "HKEY_CURRENT_USER\SOFTWARE\MICROSOFT\OFFICE.0\Excel\Security\AccessVBOM"
Sub AllowAccessToVBOM()
        With CreateObject("WScript.Shell")
            'write registry key
            .RegWrite regKey, "0", "REG_DWORD"
            MsgBox regKey & " : " & .regRead(regKey)
        End With


End Sub

回答by Cristóbal Andrés Ojeda Pinto

i was working on this and actually you CAN make it work with SendKeys and DoEvents. My following code works with 16.0 Spanish Version of Excel. Just define this with Ctrl + "m" and will run ok.

我正在研究这个,实际上你可以让它与 SendKeys 和 DoEvents 一起工作。我的以下代码适用于 16.0 西班牙语版本的 Excel。只需用 Ctrl + "m" 定义它,就可以正常运行。

Option Explicit
#If VBA7 Then
    Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#Else
    Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If

Sub VBATrust()
Dim x As Boolean, y As Boolean
Dim inicio, final

Application.OnKey "+{m}"

inicio = Now()
x = VBAIsTrusted

'If Application.LanguageSettings.LanguageID(msoLanguageIDUI) = 3082 _
'And Application.Version = "16.0" Then

    AppActivate (ThisWorkbook.Name & " - Excel")
    Application.Wait (Now() + TimeValue("00:00:03"))
    Application.SendKeys "%ao{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}%c", True
    Application.SendKeys "{UP}{UP}{UP}{UP}{UP}{UP}{UP}{UP}{UP}{UP}{UP}", True
    Application.SendKeys "{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}", True
    Application.SendKeys "{TAB}{TAB} ", True
    Application.SendKeys "~", True
    Application.SendKeys "{TAB}~", True

'End If

DoEvents
y = VBAIsTrusted
final = Now()

MsgBox inicio & " - Modelo de objetos VBA : " & x & vbNewLine & _
       final & " - Modelo de objetos VBA : " & y

End Sub

Public Function VBAIsTrusted() As Boolean
Dim a1 As Integer
On Error GoTo Label1
a1 = ActiveWorkbook.VBProject.VBComponents.Count
VBAIsTrusted = True
Exit Function
Label1:
VBAIsTrusted = False
End Function