By default, FileSystemObject is not available in VBA Excel. In case you are dealing with files and folders outside the Excel application, the first step we need to do is to create a reference to the library containing these objects (drive, file, folder).
How to activate and start using FileSystemObject in VBA
Guide to Activate FileSystemObject in VBA
There are 2 methods to activate and start using FileSystemObject in VBA:
Method 1: Set a reference to Microsoft Scripting Runtime Library (Scrrun.dll).
Method 2: Create an object to reference the library from code.
Although both methods work perfectly, Mytour advises you to apply the first method.
Note: When activating FileSystemObject, we can access all the objects within it, including FileSystemObject, Drive, Files, Folders, etc.
1. Set a reference to Microsoft Scripting Runtime Library (Scrrun.dll)
When creating a reference to the Scripting Runtime Library, we enable VBA to access all the attributes and methods of files and folders.
After completion, we can reference file / folder / driver objects directly within VBA (similarly to how we can reference cells, worksheets, ...).
Follow the steps below to create a reference to Microsoft Scripting Runtime Library:
Step 1: In the VB editor, select Tools.
Step 2: Click on References.
Step 3: In the References dialog box, scroll through the available references, locate and check the option Microsoft Scripting Runtime.
Step 4: Click on OK.
The above steps allow referencing FSO objects from VBA.
2. Create a variant of FileSystemObject in code
After setting a reference to the Scripting FileSystemObject library, the next step we need to do is to create a variant of the FSO object in the code.
Once created, we can utilize this variant in VBA.
Below is the code to set the object variable MyFSO as a FileSystemObject object:
Sub CreatingFSO()
Declare MyFSO as a FileSystemObject variable
Assign MyFSO as a new instance of FileSystemObject
End Sub
In the above code snippet, firstly we declare the variable MyFSO as an object of type FileSystemObject. We can do this because in the previous step we created a reference to the Microsoft Scripting Runtime Library. If the reference is not created, executing these steps will return an error message.
At line 2, there are 2 scenarios that can occur:
- Scenario 1: The keyword NEW creates a variant of FileSystemObject. This means we can use all the methods of FileSystemObject to work with files and folders. Without creating this variant, we cannot access the methods of FSO.
- Scenario 2: The keyword SET establishes the MyFSO object into a new variant of FileSystemObject. This allows us to use this object to access files and folders. For example, if we want to create a folder, we can use the method MyFSO.CreateFolder.
Additionally, if desired, we can combine the above 2 commands into one as follows:
In the CreatingFSO subroutine:
Declare MyFSO as a FileSystemObject variable
End the subroutine
The advantage of using this method (setting a reference to the Microsoft Scripting Runtime Library) is that when using FSO objects in the code, we can utilize the IntelliSense feature to display the methods and properties associated with the object (as shown in the image below).
This is not possible if we create a reference within the code.
3. Create a FileSystemObject object directly in the code
Another way to reference FSO is to do it directly in the code. With this method, we don't need to create any references (similar to the method above).
During the coding process, we can create an object directly within the code and reference Scripting.FileSystemObject.
The code snippet below creates an FSO object and then sets this object to be of type FileSystemObject:
Sub FSODemo()
Declare FSO As Object
Set FSO = CreateObject('Scripting.FileSystemObject')
Concluding the subroutine
Although this method may seem faster, its major drawback is the lack of IntelliSense when working with objects in FSO.
Above is the tutorial on activating FileSystemObject in VBA. Additionally, don't miss other articles on the same topic by Mytour such as:
- Understanding FileSystemObject (FSO) in VBA Part 1
- Understanding FileSystemObject (FSO) in VBA Part 2