开发者

Is it possible to request temporary trusted access to VBA project object model?

开发者 https://www.devze.com 2022-12-14 04:17 出处:网络
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 t

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...)?


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号