我需要为我的客户执行以下代码行,问题是为了访问它们我必须在项目中激活某个选项,有没有办法避免这种情况?
Application.VBE.ActiveVBProject.References.AddFromFile "C:\Windows\System32\mshtml.tlb"
Application.VBE.ActiveVBProject.References.AddFromFile "C:\Windows\System32\msxml6.dll"
它们是执行我的宏所必需的几个库。
我正在运行的宏如下:
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
执行我的宏所需的库
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?
该选项不在项目中,它在主机应用程序级别(Excel):是否信任对 VBIDE API 的编程访问是宏安全设置的重要组成部分,不,没有任何办法解决它.
任何规避此安全设置的编程方式都会造成世界上每个宏病毒都会跳入的巨大安全漏洞。
但你不需要这样做。您的代码正在使用
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
使用指定的 ProgID 字符串在 Windows 注册表中查找注册,定位类型库,创建该类型的实例,并返回对该对象的引用 - 一切都在运行时解决(因此“延迟”绑定(bind)),并且只要 ProgID 存在于注册表中并且后期绑定(bind)代码中没有错字(Option Explicit
无法将您从这些错误中拯救出来),一切都“正常工作”。如果您使用
New
而是关键字...With New MSXML2.XMLHTTP ' returns an XMLHTTP reference
.Open ... 'early-bound member call (with intelilsense and compile-time validation)
'...
End With
如果没有对
MSXML2
的项目引用,则无法编译。图书馆。
关于excel - Vba Excel 信任访问 VBA 项目模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57577687/