迟绑定全局变量?(Late Binding Global Variables?)

编程入门 行业动态 更新时间:2024-10-10 12:21:47
绑定全局变量?(Late Binding Global Variables?)

我正在使用Excel的VBA。 根据我的理解,全局变量需要在任何子目录之外声明。 这是他们可以被所有潜艇访问的唯一途径。

与此同时,我想做后期绑定以引用“Microsoft Scripting Runtime”库(以便使用字典对象类型),以便最终用户不必亲自去做。

我的代码如下:

On Error Resume Next strGUID = "{420B2830-E718-11CF-893D-00A0C9054228}" ThisWorkbook.VBProject.References.AddFromGuid GUID:=strGUID, Major:=1, Minor:=0 Dim Dic1 As Object Set Dic1 = CreateObject("Scripting.Dictionary") Dim Dic2 As Object Set Dic2 = CreateObject("Scripting.Dictionary")

如果我想用迟绑定来声明全局字典对象呢? 它看起来像VBA不会允许我把任何代码(除了声明之外)。

我怎样才能声明一个全局字典对象,而不需要他自己的最终用户配置库引用? 我应该不要以下?

Dim Dic1 As Object Dim Dic2 As Object Sub Prog1() On Error Resume Next strGUID = "{420B2830-E718-11CF-893D-00A0C9054228}" ThisWorkbook.VBProject.References.AddFromGuid GUID:=strGUID, Major:=1, Minor:=0 Set Dic1 = CreateObject("Scripting.Dictionary") Set Dic2 = CreateObject("Scripting.Dictionary") End Sub

I'm using VBA for Excel. From my understanding, global variables need to be declared outside of any subs. That's the only way they can be accessed by all subs.

At the meantime, I want to do late binding to reference the "Microsoft Scripting Runtime" library(in order to use the dictionary object type) so that an end user doesn't have to do it himself.

My code is as below:

On Error Resume Next strGUID = "{420B2830-E718-11CF-893D-00A0C9054228}" ThisWorkbook.VBProject.References.AddFromGuid GUID:=strGUID, Major:=1, Minor:=0 Dim Dic1 As Object Set Dic1 = CreateObject("Scripting.Dictionary") Dim Dic2 As Object Set Dic2 = CreateObject("Scripting.Dictionary")

What if I want to declare global dictionary object with late binding? It looks like VBA won't allow me to put any code outside of the subs (other than the declarations).

How may I declare a global dictionary object without needing the end user configure library reference himself? Shall I don the following?

Dim Dic1 As Object Dim Dic2 As Object Sub Prog1() On Error Resume Next strGUID = "{420B2830-E718-11CF-893D-00A0C9054228}" ThisWorkbook.VBProject.References.AddFromGuid GUID:=strGUID, Major:=1, Minor:=0 Set Dic1 = CreateObject("Scripting.Dictionary") Set Dic2 = CreateObject("Scripting.Dictionary") End Sub

最满意答案

与VBA代码本身一样,当用户打开主机工作簿时,项目引用不会奇迹般地消失。 它们与代码一起保存在主机文档中。

所以,你的问题的前提是错误的:用户永远不需要调整项目引用。

此外,脚本运行时类型库是标准问题,并且在本世纪内建立的每台Windows计算机上都已发布完全相同的版本(甚至在此之前),这意味着除非您的代码需要在Mac上运行,否则无需延迟 - 绑定脚本运行时库。

如果您的代码需要在Mac上运行,则该库无论如何都不会延迟绑定,因为它不会在主机上找到,所以迟到的脚本运行时绑定只会造成无聊的拼写错误并引入其他易于避免的IntelliSense有助于预防的错误。


ThisWorkbook.VBProject.References.AddFromGuid GUID:=strGUID, Major:=1, Minor:=0

这违背了后期绑定的整个目的:它使用VBIDE扩展性库(它需要降低宏安全设置)以编程方式添加一个引用,您可以在设计时通过VBE的工具菜单轻松添加该引用。

后期绑定的代码根本不需要引用。 不是在编译时,而是在运行时。

添加引用,保存,然后将对象声明As Scripting.Dictionary并享受早期绑定代码的好处。

Set Dic1 = New Scripting.Dictionary

这就是你需要的。


如果我想用迟绑定来声明全局字典对象呢? 它看起来像VBA不会允许我把任何代码(除了声明之外)。

晚结合与该方面的早结合没有任何不同。 晚期代码和早期代码之间的唯一区别是声明的As子句:

Private foo As Object ' no compile-time type knowledge: late-bound Private bar As Dictionary ' compile-time type knowledge: early-bound

如何初始化该对象引用对声明的后期/早期绑定性质没有任何影响。

这在注册表中查找ProgID以查找库和类型:

Set foo = CreateObject("Scripting.Dictionary")

这使用项目引用:

Set foo = New Scripting.Dictionary

两者都是正确的,两者都会对早期或迟到的声明起作用。 除此之外,如果您已经有了对类型库的引用,那么确实不需要去注册表来找到该类库 - 只需将其更新即可!

Like the VBA code itself, project references don't magically disappear when your user opens your host workbook. They're saved along with the code in the host document.

So, the premise of your question is wrong: users never need to tweak project references.

Also the Scripting Runtime type library is standard issue and has been shipped the exact same version on every single Windows machine built this century (even before that), which means unless your code needs to run on a Mac, there's no need to ever late-bind the Scripting Runtime library.

And if your code needs to run on a Mac, the library won't late-bind anyway because it won't be found on the host machine, so late-binding the Scripting Runtime only serves to make silly typos and introduce other easily avoidable bugs that IntelliSense helps preventing.


ThisWorkbook.VBProject.References.AddFromGuid GUID:=strGUID, Major:=1, Minor:=0

This defeats the entire purpose of late-binding: it's using the VBIDE extensibility library (which requires lowered macro security settings) to programmatically add a reference that you can easily add at design-time through the VBE's Tools menu.

Late-bound code doesn't need the reference at all. Not at compile-time, not at run-time.

Add the reference, save, then declare your objects As Scripting.Dictionary and enjoy the benefits of early-bound code.

Set Dic1 = New Scripting.Dictionary

That's all you need.


What if I want to declare global dictionary object with late binding? It looks like VBA won't allow me to put any code outside of the subs (other than the declarations).

Late binding isn't any different than early binding in that aspect. The only difference between late and early bound code is the As clause of the declaration:

Private foo As Object ' no compile-time type knowledge: late-bound Private bar As Dictionary ' compile-time type knowledge: early-bound

How you're initializing that object reference makes no difference to the late/early binding nature of the declaration.

This looks up a ProgID in the registry to find the library and the type:

Set foo = CreateObject("Scripting.Dictionary")

This uses the project references:

Set foo = New Scripting.Dictionary

Both are correct, and both will work against either early or late-bound declarations. Except, if you already have a reference to the type library, there's not really a need to go hit the registry to locate that library - just New it up!

更多推荐

本文发布于:2023-08-04 02:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1408543.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   全局变量   Late   Variables   Global

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!