是否可以请求对 VBA 项目对象模型的临时可信访问?

发布于 2024-08-14 23:10:47 字数 426 浏览 2 评论 0原文

我需要通过.Net / C#访问Office文档(Excel工作簿,但不相关)的VBA代码。我知道如何执行此操作,但这需要 Office 用户通过 Office 应用程序授予对 VBA 项目对象模型的受信任访问权限。
这让我感到不舒服,因为存在用户以这种方式设置的风险,这是不可取的,并且因为这需要用户在未授予访问权限的情况下启动 Office 应用程序并更改设置,这并不令人愉快为用户。
我相信.Net代码不能自动更改该设置(这很好),但是有没有办法询问用户是否想要暂时授予授权?或者有没有办法在安装后专门为我的应用程序提供对 VBE 的访问?
我的假设是这些都不可行,但我认为如果有人知道答案,他/她就会出现在 StackOverflow 上:)
作为一个额外的问题,有谁知道如何以编程方式检查 Office 应用程序是否已授予对 VBA 项目对象模型的访问权限(没有 try/catch 那...)?

I need to access the VBA code of Office documents (Excel workbooks, but it's not relevant) through .Net / C#. I know how to do this, but this requires the Office user to have granted trusted access to the VBA project object model through the Office app.
This makes me uncomfortable, because there is a risk that the user leaves things set that way, which is not desirable, and because this requires the user to fire the Office app and change the settings if access has not been granted, which is not pleasant for the user.
I believe .Net code cannot change that setting automatically (which is good), but is there a way to ask the user if he/she wants to temporarily grant authorization? Or is there a way to give access to the VBE specifically to my application when it gets installed?
My assumption is that none of these are feasible, but I thought that if someone knew the answer, he/she would be on StackOverflow :)
As a bonus question, does anyone know how to programmatically check whether an Office app has granted access to the VBA project object model (without a try/catch that is...)?

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

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

发布评论

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

评论(1

白昼 2024-08-21 23:10:47

遗憾的是,您可以通过修改与安全相关的注册表项来实现您的要求。您可以设置注册表项,执行所需的任务,然后重新设置注册表项,如下例所示。

  Public Sub ModifyVBA()
    Set wsh = CreateObject("WScript.Shell")
    'key to modify
    keyName = "HKEY_LOCAL_MACHINE\Software\Microsoft\Office\" & _
        Application.Version & "\Excel\Security\AccessVBOM"
    'enable access
    wsh.RegWrite keyName, 1, "REG_DWORD"
    'read the vba project name
    Application.VBE.ActiveVBProject.VBComponents.Add (vbext_ct_ClassModule)
    'disable access
    wsh.RegDelete keyName
End Sub

(免责声明:我想我最初是从另一个论坛摘取此内容的)。

Sadly what you're requesting is possible by modifying the registry keys relating to security. You can set the registry key, perform the tasks that you require, then set the registry key back, as in the example below.

  Public Sub ModifyVBA()
    Set wsh = CreateObject("WScript.Shell")
    'key to modify
    keyName = "HKEY_LOCAL_MACHINE\Software\Microsoft\Office\" & _
        Application.Version & "\Excel\Security\AccessVBOM"
    'enable access
    wsh.RegWrite keyName, 1, "REG_DWORD"
    'read the vba project name
    Application.VBE.ActiveVBProject.VBComponents.Add (vbext_ct_ClassModule)
    'disable access
    wsh.RegDelete keyName
End Sub

(Disclaimer: I think I originally lifted this from another forum).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文