简体   繁体   English

Vba Excel Trust对VBA项目模型的访问

[英]Vba Excel Trust access to the VBA project model

I need to execute the following lines of code for my client, the problem is that in order to access them I must have activated a certain option within the project, is there any way to avoid that? 我需要为客户端执行以下代码行,问题是要访问它们,我必须在项目内激活某个选项,是否有任何方法可以避免这种情况?

图片

Application.VBE.ActiveVBProject.References.AddFromFile "C:\Windows\System32\mshtml.tlb"
Application.VBE.ActiveVBProject.References.AddFromFile "C:\Windows\System32\msxml6.dll"

They are a couple of libraries necessary for the execution of my Macro. 它们是执行我的宏所必需的几个库。

The macro that I am running is as follows: 我正在运行的宏如下:

Sub GetCurrentDate()
    Dim S$

    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "http://www.fechadehoy.com/venezuela", False
        .send
        S = .responseText
    End With

    With CreateObject("htmlfile")
        .body.innerHTML = S
        MsgBox .getElementById("fecha").innerText
    End With
End Sub

libraries needed for the execution of my macro 执行我的宏所需的库

Microsoft XML, v6.0
Microsoft HTML Object Library

in order to access them I must have activated a certain option within the project, is there any way to avoid that? 为了访问它们,我必须在项目中激活某个选项,有什么方法可以避免这种情况?

The option isn't within the project , it's at the host application level (Excel): whether programmatic access to the VBIDE API is trusted or not is an important part of macro security settings, and no, there isn't any way around it. 该选项不在项目内 ,而是在主机应用程序级别(Excel)上:能否信任对VBIDE API的编程访问是宏安全设置的重要组成部分,并且不存在任何解决方案。

Any programmatic way to circumvent this security setting would make a huge gaping security hole every macro-virus in the world would jump into. 任何以编程方式规避此安全设置的方​​法,都会使世界上的每种宏病毒都跳进一个巨大的安全漏洞。


But you don't need to do this. 但是您不需要这样做。 Your code is creating instances of classes located in these libraries using CreateObject , not the New operator: the references are not needed. 您的代码正在使用CreateObject而不是New运算符创建位于这些库中的类的实例:不需要引用。

With CreateObject("MSXML2.XMLHTTP") ' returns an Object/XMLHTTP reference
    .Open ...  'late-bound member call (no intellisense, no compile-time validation)
    '...
End With

CreateObject uses the specified ProgID string to find the registration in the Windows Registry, locate the type library, create an instance of the type, and return a reference to that object - everything is resolved at run-time (hence "late" bound), and as long as the ProgID exists in the registry and there's no typo in the late-bound code ( Option Explicit can't save you from those), everything "just works". CreateObject使用指定的ProgID字符串在Windows注册表中查找注册,找到类型库,创建该类型的实例,并返回对该对象的引用-一切都在运行时解析(因此“后期”绑定),只要ProgID存在于注册表中,并且后期绑定代码中没有错字( Option Explicit不能将您从中救出来),一切都会“正常”。

If you used the New keyword instead... 如果您改用New关键字...

With New MSXML2.XMLHTTP ' returns an XMLHTTP reference
    .Open ... 'early-bound member call (with intelilsense and compile-time validation)
    '...
End With

This couldn't be compiled without a project reference to the MSXML2 library. 没有对MSXML2库的项目引用,就无法编译此文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM