问题描述
我应该用什么样的标准来决定是否我编写VBA code是这样的:
What criteria should I use to decide whether I write VBA code like this:
Set xmlDocument = New MSXML2.DOMDocument
或者是这样的:
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")
是相同的
Dim xmlDocument as MSXML2.DOMDocument
Set xmlDocument = New MSXML2.DOMDocument
都使用早期绑定。而
both use early binding. Whereas
Dim xmlDocument as Object
Set xmlDocument = CreateObject("MSXML2.DOMDocument")
使用后期绑定。请参阅MSDN href=\"http://support.microsoft.com/kb/245115\">。
uses late binding. See MSDN here.
当您创建外部提供的对象,也有新的运营商之间没有差异,声明一个变量如新,并且使用CreateObject函数。
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.
新要求,类型库引用。而使用的CreateObject注册表。
New requires that a type library is referenced. Whereas CreateObject uses the registry.
的CreateObject可用于在远程机器上创建一个对象
CreateObject can be used to create an object on a remote machine.
这篇关于什么是使用在Excel VBA的新的关键字和调用的CreateObject之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!