在VBA上声明和定义FileSystemObject对象的正确方法是什么? [英] What is the correct way to declare and define a FileSystemObject object on VBA?

查看:242
本文介绍了在VBA上声明和定义FileSystemObject对象的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关如何声明FileSystemObjects对象的信息,但发现令人困惑的信息.是因为有不同的声明方式吗?

I was reading about how to declare FileSystemObjects objects and I found confusing information. Is it because there are different ways to declare it?

我让您发现一些声明和定义FileSystemOjbect对象的方法:

I let you some of the ways I found to declare and define FileSystemOjbect objects:

  1. Dim FSO As FileSystemObject Set FSO = New FileSystemObject

  1. Dim FSO As FileSystemObject Set FSO = New FileSystemObject

Dim FSO As New FileSystemObject

Dim FSO As Object Set FSO = CreateObject("scripting.filesystemobject")

Dim FSO As Object Set FSO = CreateObject("scripting.filesystemobject")

哪种是声明FileSystemObject对象的正确方法?

Which is the right way to declare FileSystemObject objects?

推荐答案

所有3种方法都是正确的.您已经找到了两种使用对象的方法.

All 3 ways are correct. You have hit 2 different approaches to using objects.

  • 前两种方式表示早期绑定".
  • 最后一种方式表示后期绑定".

中间方法是第一种方法的捷径,但并不完全. 新手VBA用户最好不要使用复杂的代码, 因为对对象变量的任何引用都会创建该对象的新实例, 如果对象变量= Nothing

The middle way is about a shortcut to the the 1st way, but not fully. It is better avoided by novice VBA users in complex code, as any reference to the object variable creates a new instance of the object, if the object variable=Nothing

早期绑定: 必须链接VBA中的已用库/模块-工具-参考, 在这段时间内 Microsoft脚本运行时库 如果目标计算机上不存在模块/代码,则执行将失败. 据报道,早期结合明显更快. 早期绑定在开发过程中提供了Intellisense-editor关于对象方法和属性以及命名常量的建议

Early binding: One has to have linked the used libraries/modules in VBA - Tools - References, in this time Microsoft Scripting Runtime Library If the module/code is not present on the target computer, execution will fail. Early binding is reportedly significantly faster. Early binding offers at development the Intellisense-editor suggestion of object methods and properties and named constants

后期绑定: 无需链接使用的外部库/模块-更好的机器间可移植性. 据报道,后期装订较慢. 后期绑定不提供Intellisense,并且必须通过其值提供显式声明的特定于对象的常量.

Late Binding: No need of linking used external libraries/modules - better intermachine portability. Late binding is reportedly slower. Late binding does not offer Intellisense and the object specific constants has to be either explicitly declared either provided by their value.

例如参见基于项目范围的条件编译参数Earlybinding的条件代码编译:

See e.g. a conditional code compilation, based on the Project-wide conditional compilation argument Earlybinding :

Sub EarlyVsLateBindingtest()

#If Earlybinding Then
   Dim oFS As Scripting.FileSystemObject
   Set oFS = New Scripting.FileSystemObject
#Else
   Const TemporaryFolder = 2
   Dim oFS As Object
   Set oFS = CreateObject("Scripting.FileSystemObject")
#End If

oFS.GetSpecialFolder (TemporaryFolder)

End Sub

https://superuser.com/questions/615463/how-to-avoid-references-in-vba-early-binding-vs-late-binding/1262353?noredirect=1#comment1859095_1262353

另请参见

https://wordmvp.com/FAQs/InterDev/EarlyvsLateBinding.htm

https ://support.microsoft.com/en-gb/help/245115/using-early-binding-and-late-binding-in-automation

这篇关于在VBA上声明和定义FileSystemObject对象的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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