在 Excel VBA 中使用 New 关键字和调用 CreateObject 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/170070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are the differences between using the New keyword and calling CreateObject in Excel VBA?
提问by Matthew Murdoch
What criteria should I use to decide whether I write VBA code like this:
我应该使用什么标准来决定是否编写这样的 VBA 代码:
Set xmlDocument = New MSXML2.DOMDocument
or like this:
或者像这样:
Set xmlDocument = CreateObject("MSXML2.DOMDocument")
?
?
采纳答案by Mitch Wheat
As long as the variable is not typed as object
只要变量不是对象类型
Dim xmlDocument as MSXML2.DOMDocument
Set xmlDocument = CreateObject("MSXML2.DOMDocument")
is the same as
是相同的
Dim xmlDocument as MSXML2.DOMDocument
Set xmlDocument = New MSXML2.DOMDocument
both use early binding. Whereas
两者都使用早期绑定。然而
Dim xmlDocument as Object
Set xmlDocument = CreateObject("MSXML2.DOMDocument")
uses late binding. See MSDN here.
使用后期绑定。请参阅此处的MSDN 。
When you're creating externally provided objects, there are no differences between the New operator, declaring a variable As New, and using the CreateObject function.
当您创建外部提供的对象时,New 运算符、将变量声明为 New 和使用 CreateObject 函数之间没有区别。
New requires that a type library is referenced. Whereas CreateObject uses the registry.
New 要求引用类型库。而 CreateObject 使用注册表。
CreateObject can be used to create an object on a remote machine.
CreateObject 可用于在远程机器上创建对象。
回答by JimmyPena
You should always use
你应该总是使用
Set xmlDocument = CreateObject("MSXML2.DOMDocument")
Set xmlDocument = CreateObject("MSXML2.DOMDocument")
This is irrelevant to the binding issue. Only the declaration determines the binding.
这与绑定问题无关。只有声明决定了绑定。
Using CreateObject
exclusively will make it easier to switch between early and late binding, since you only have to change the declaration line.
CreateObject
独占使用将使在早期和晚期绑定之间切换更容易,因为您只需要更改声明行。
In other words, if you write this:
换句话说,如果你这样写:
Dim xmlDocument As MSXML2.DOMDocument
Set xmlDocument = CreateObject("MSXML2.DOMDocument")
Dim xmlDocument As MSXML2.DOMDocument
Set xmlDocument = CreateObject("MSXML2.DOMDocument")
Then, to switch to late binding, you only have to change the first line (to As Object
).
然后,要切换到后期绑定,您只需更改第一行(到As Object
)。
If you write it like this:
如果你这样写:
Dim xmlDocument As MSXML2.DOMDocument
Set xmlDocument = New MSXML2.DOMDocument
Dim xmlDocument As MSXML2.DOMDocument
Set xmlDocument = New MSXML2.DOMDocument
then when you switch to late binding, you have to change both lines.
然后当您切换到后期绑定时,您必须更改这两行。
回答by Joe
For the former you need to have a reference to the type library in your application. It will typically use early binding (assuming you declare your variable as MSXML2.DOMDocument rather than as Object, which you probably will), so will generally be faster and will give you intellisense support.
对于前者,您需要引用应用程序中的类型库。它通常会使用早期绑定(假设您将变量声明为 MSXML2.DOMDocument 而不是对象,您可能会这样做),因此通常会更快,并且会为您提供智能感知支持。
The latter can be used to create an instance of an object using its ProgId without needing the type library. Typically you will be using late binding.
后者可用于使用其 ProgId 创建对象的实例,而无需类型库。通常,您将使用后期绑定。
Normally it's better to use "As New" if you have a type library, and benefit from early binding.
通常,如果您有类型库,最好使用“As New”,并从早期绑定中受益。