5

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

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

  1. Dim FSO As FileSystemObject Set FSO = New FileSystemObject

  2. Dim FSO As New FileSystemObject

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

Which is the right way to declare FileSystemObject objects?

Mauro
  • 477
  • 1
  • 9
  • 22
  • 1
    It depends. 1&2 requires early binding and a reference, while 3 is correct if you are using late binding. 1 will initiate the FSO only on the Set FSO... line, while 2 will initiate it the moment it get's declared (i.e. when your sub starts). – tsdn Apr 27 '18 at 11:33
  • @JK2017 What do you mean exactly with "late binding" and "early binding", are you meaning binding the object to a FileSystemObject object class? – Mauro Apr 27 '18 at 11:45
  • 1
    See http://excelmatters.com/2013/09/23/vba-references-and-early-binding-vs-late-binding/ and https://support.microsoft.com/en-us/help/245115/using-early-binding-and-late-binding-in-automation for more info about late versus early and there are lots of existing questions on this on SO. – QHarr Apr 27 '18 at 11:53
  • 1
    Note that number 2 uses autoinstantiation https://stackoverflow.com/questions/21652671/what-does-the-keyword-new-do-in-vba?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – QHarr Apr 27 '18 at 11:54

1 Answers1

4

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

  • The former 2 ways mean "Early Binding".
  • The last way means "Late Binding".

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

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

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.

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

See also

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

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

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
Poutnik
  • 191
  • 7