Question about CreateObject() in VB6 / VBA
Asked Answered
S

3

13

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?

Sacha answered 5/12, 2008 at 21:19 Comment(0)
B
25

It is the ProgID of the component which is registered in Windows registry under HKCR key:

HKEY_CLASSES_ROOT\Scripting.FileSystemObject

ProgID's are human readable identifiers for COM objects. They point to the actual CLSIDs, which in this case is:

HKEY_CLASSES_ROOT\CLSID\{0D43FE01-F093-11CF-8940-00A0C9054228}

This is the place where you can find the actual COM .dll that includes the implementation of the component.

In the first sample code you have provided you are doing an early-binding, and in the second one you are doing a late-binding.

Bourse answered 5/12, 2008 at 21:22 Comment(0)
K
3

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

Kinghood answered 9/12, 2008 at 10:55 Comment(0)
C
1

I would start by searching for FileSystemObject in the MSDN library at http://msdn.microsoft.com/library

The site is chock full of documentation, including the details of how to call CreateObject.

Conclude answered 5/12, 2008 at 21:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.