问题描述
我应该使用什么标准来决定是否写这样的VBA代码:
设置xmlDocument =新建MSXML2.DOMDocument
或像这样:
设置xmlDocument = CreateObject(MSXML2.DOMDocument)
?
只要变量没有键入对象
Dim xmlDocument as MSXML2.DOMDocument
设置xmlDocument = CreateObject(MSXML2.DOMDocument)
与
Dim xmlDocument as MSXML2.DOMDocument
设置xmlDocument =新的MSXML2相同。 DOMDocument
都使用早期绑定。而$ p
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / code>
使用后期绑定。请参阅MSDN 。
当您'创建外部提供的对象,新操作符之间没有区别,声明变量As New,并使用CreateObject函数。
新的要求类型库是引用。而CreateObject使用注册表。
CreateObject可用于在远程计算机上创建对象。
What criteria should I use to decide whether I write VBA code like this:
Set xmlDocument = New MSXML2.DOMDocument
or like this:
Set xmlDocument = CreateObject("MSXML2.DOMDocument")
?
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.
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 requires that a type library is referenced. Whereas CreateObject uses the registry.
CreateObject can be used to create an object on a remote machine.
这篇关于在Excel VBA中使用New关键字和调用CreateObject有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!