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

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

问题描述

我应该使用什么标准来决定我是否编写这样的 VBA 代码:

What criteria should I use to decide whether I write VBA code like this:

Set xmlDocument = New MSXML2.DOMDocument

或者像这样:

Set xmlDocument = CreateObject("MSXML2.DOMDocument")

?

推荐答案

只要变量不是对象类型

Dim xmlDocument as MSXML2.DOMDocument
Set xmlDocument = CreateObject("MSXML2.DOMDocument")

Dim xmlDocument as MSXML2.DOMDocument
Set xmlDocument = New MSXML2.DOMDocument

都使用早期绑定.而

Dim xmlDocument as Object
Set xmlDocument = CreateObject("MSXML2.DOMDocument")

使用后期绑定.请参阅 MSDN 此处.

uses late binding. See MSDN here.

当您创建外部提供的对象时,New 运算符、将变量声明为 New 和使用 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.

New 要求引用类型库.而 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 中使用 New 关键字和调用 CreateObject 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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