I can do this:
Dim fso As New FileSystemObject
or I can do this:
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
How do I know what string to use for CreateObject? For example, how would I know to use the "Scripting." part of "Scripting.FileSystemObject"? Where do you go to look that up?
Use CreateObject when there is no current instance of the object. If an instance of the object is already running, a new instance is started, and an object of the specified type is created. To use the current instance, or to start the application and have it load a file, use the GetObject function.
In VBA, the CreateObject function is used to reference objects. Create Object function causes a late-binding process. Using the create object function, we don't get to access the IntelliSense list of VBA.
You can declare an object variable with the Object data type when the specific object type is not known until the procedure runs. Use the Object data type to create a generic reference to any object. Dim MyObject As Object ' Declared as generic object. Dim MyObject As Sample ' Declared only as Sample object.
Using the VB6 IDE, choose Project, References, then to pick the reference 'Microsoft Scripting Runtime'.
If you didn't know what the reference is called, you could use the References dialog's Browse button to pick the file /system 32/scrrun.dll.
With the reference chosen, close the References dialog then open the Object Browser (View menu). Change the dropdown to the most likely candidate, being 'Scripting'. This will reveal the library's classes, one of which is 'FileSystemObject'. Hence, you will have discovered the the string required for CreateObject is 'Scripting.FileSystemObject'.
If you didn't know the Reference name or the file name but you did know the class name then you could search the registry for "FileSystemObject" and it should soon be revealed that the fully-qualified name you require is 'Scripting.FileSystemObject'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With