Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question about CreateObject() in VB6 / VBA

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?

like image 319
Shane Miskin Avatar asked Dec 05 '08 21:12

Shane Miskin


People also ask

How does CreateObject work?

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.

What does CreateObject do in VBA?

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.

How do I declare an object in 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.


1 Answers

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'.

like image 138
onedaywhen Avatar answered Sep 26 '22 03:09

onedaywhen