在Excel VBA中使用New关键字和调用CreateObject有什么区别? [英] What are the differences between using the New keyword and calling CreateObject in Excel VBA?

查看:449
本文介绍了在Excel VBA中使用New关键字和调用CreateObject有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该使用什么标准来决定是否写这样的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有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆