vba 初始化字典早绑定和晚绑定
vba initializing a dictionary early and late binding
以下代码适用于 VBA
Dim dict_var As Dictionary
Set dict_var = CreateObject("Scripting.Dictionary")
阅读堆栈中的多个问答我多次遇到代码开头的解决方案:
Dim aDictionary As Dictionary
Set aDictionary = New Dictionary
这个最新版本给我一个错误:
新关键字的使用无效
为什么?这里的诀窍在哪里?
为什么使用一种或另一种方法?
谢谢
EDIT:
There are many questions relating to this issue in stack, this one was suggested.
Nevertheless such question relates to a much specific problem of dictionaries of dictionaries.
And it does not explain neither what are the implications of early and non-early binding.
It would be nice to know in which particular case the following works.
Dim aDictionary As Dictionary
或者换句话说,当确实有必要时:
Dim aDictionary As scripting.Dictionary
编辑2:
这些是我的图书馆:
首先,您的两个代码集都是早期绑定的。要后期绑定,您可以将变量声明为 Object
.
其次,关于具体错误,我怀疑您将引用设置为两个库,其中包含 Dictionary
class - 例如 Word 和脚本运行时 - 并且引用列表中较高的那个是单个实例对象,因此您不能将 New
与它一起使用。
以下代码适用于 VBA
Dim dict_var As Dictionary
Set dict_var = CreateObject("Scripting.Dictionary")
阅读堆栈中的多个问答我多次遇到代码开头的解决方案:
Dim aDictionary As Dictionary
Set aDictionary = New Dictionary
这个最新版本给我一个错误:
新关键字的使用无效
为什么?这里的诀窍在哪里? 为什么使用一种或另一种方法?
谢谢
EDIT: There are many questions relating to this issue in stack, this one was suggested. Nevertheless such question relates to a much specific problem of dictionaries of dictionaries. And it does not explain neither what are the implications of early and non-early binding. It would be nice to know in which particular case the following works.
Dim aDictionary As Dictionary
或者换句话说,当确实有必要时:
Dim aDictionary As scripting.Dictionary
编辑2:
这些是我的图书馆:
首先,您的两个代码集都是早期绑定的。要后期绑定,您可以将变量声明为 Object
.
其次,关于具体错误,我怀疑您将引用设置为两个库,其中包含 Dictionary
class - 例如 Word 和脚本运行时 - 并且引用列表中较高的那个是单个实例对象,因此您不能将 New
与它一起使用。