10/20/2019 Excel Vba Microsoft Scripting Runtime
Predominantly about automating Microsoft Office and building MS Office tools and applications. The occasional post may well cover over areas of ICT Zypher.co.uk The Blog: Useful VBA file functions - Microsoft Scripting Runtime.
When we use VBA in Excel, most of it is to automate our tasks.
This also means that most of the time, we work with cells and ranges, worksheets, workbooks, and other objects which are a part of the Excel application.
But VBA is a lot more powerful and can also be used to work with stuff outside of Excel.
In this tutorial, I will show you how to use VBA FileSystemObject (FSO) to work with files and folders on your system or network drives.
What is VBA FileSystemObject (FSO)?
FileSystemObject (FSO) allows you to access the file system of your computer. Using it, you can access and modify the files/folders/directories in your computer system.
For example, below are some of the things you can do by using FileSystemObject in Excel VBA:
I hope you get the idea.
I will cover all these above examples (plus more) later in this tutorial.
While some of the things mentioned above can also be done using traditional VBA functions (such as the DIR function) and methods, that would lead to longer and more complicated codes. FileSystemObject makes it easy to work with files and folders while keeping the code clean and short.
What All Objects Can You Access Through FileSystemObject?
As I mentioned above, you can access and modify files and folders using the FileSystemObject in VBA.
Below is a table that shows the most important objects that you can access and modify using FSO:
Each of the above objects has methods that you can use to work with these.
To give you an example, if you want to delete a folder, you will use the DeleteFolder method of the Folder object. Similarly, if you want to copy a file, you will use the CopyFile method of the File object.
Don’t worry if this seems overwhelming or hard to understand. You will get a much better understanding when you go through the examples that I have covered in this tutorial.
Just for the reference purpose, I have covered all the FileSystemObject methods (for each object) at the end of this tutorial.
Enabling FileSystemObject in Excel VBA
FileSystemObject is not available by default in the Excel VBA.
Since we are dealing with files and folders that are outside of the Excel application, we need to first create a reference to the library that holds these objects (drives, files, folders).
Now there are two ways you can start using FileSystemObject in Excel VBA:
While both these methods work (and I’ll show you how to do this next), I recommend using the first method.
Note: When you enable FileSystemObject, you can access all the objects in it. This includes the FileSystemObject, Drive, Files, Folders, etc. I will be focussing majorly on the FileSystemObject in this tutorial.
Setting the Reference to the Microsoft Scripting Runtime Library
When you create a reference to the Scripting Runtime Library, you allow Excel VBA the access to all the properties and methods of files and folder. Once this is done, you can refer to the files/folders/drives object from within the Excel VBA (just like you can refer the cells, worksheets or workbooks).
Below are the steps to create a reference to the Microsoft Scripting Runtime Library:
The above steps would now allow you to refer to the FSO objects from Excel VBA.
Creating an Instance of FileSystemObject in the Code
Once you have set the reference to the Scripting FileSystemObject library, you need to create an instance of the FSO object in your code.
Once this is created, you can use it in VBA.
Below is the code that will set the object variable MyFSO as a FileSystemObject object:
In this code, first I have declared the variable MyFSO as a FileSystemObject type object. This is possible only because I have created a reference to the Microsoft Scripting Runtime Library. If the reference is not created, this is going to give you an error (as Excel wouldn’t recognize what FileSystemObject means).
In the second line, two things happen:
If you want, you can also combine the above two statements into one as shown below:
A big benefit of using this method (which is to set the reference to the Microsoft Scripting Runtime Library) is that when you use the FSO objects in your code, you will be able to use the IntelliSense feature that shows the methods and properties associated with an object (as shown below).
This is not possible when you create the reference from within the code (covered next).
Creating an Object from the Code
Another way to create a reference to FSO is by doing it from the code. In this method, you don’t need to create any reference (as done in the previous method).
When you are writing the code, you can create an object from within the code and refer to the Scripting.FileSystemObject.
The below code creates an object FSO and then makes this a FileSystemObject type.
While this may seem more convenient, a big downside of using this method is that it would not show an IntelliSense when you work with objects in FSO. For me, this is a huge negative and I always recommend using the previous method of enabling FSO (which is by setting the reference to the ‘Microsoft Scripting Runtime’)
VBA FileSystemObject Examples
Now let’s dive in and have a look at some practical examples of using FileSystemObject in Excel.
Example 1: Check if a File or Folder Exists
The following code will check whether the folder with the name ‘Test’ exists or not (in the specified location).
If the folder exists, the IF condition is True and it shows a message – ‘The Folder Exists’ in a message box. And if it doesn’t exist, it shows a message – The Folder Does Not Exist’.
Similarly, you can also check if a file exists or not.
The below code checks whether there is a file with the name Test.xlsx in the specified folder or not.
Example 2: Create a New Folder in the Specified Location
The below code would create a folder with the name ‘Test’ in the C drive of my system (you will have to specify the path on your system where you want to create the folder).
While this code works fine, it would show an error in case the folder already exists.
The below code checks whether the folder already exists and creates a folder if it doesn’t. In case the folder already exists, it shows a message. To check whether the folder exists, I have used the FolderExists method of the FSO.
Example 3: Get a List of All Files in a Folder
The below code would show the names of all the files in the specified folder.
This code is a little more complex that the ones we have already seen.
As I mentioned above in this tutorial, when you reference the ‘Microsoft Scripting Runtime Library’, you can use FileSystemObject as well as all other objects (such as Files and Folders).
In the above code, I use three objects – FileSystemObject, File, and Folder. This allows me to go through each file in the specified folder. I then use the name property to get the list of all file names.
Note that I am using Debug.Print to get the names of all the files. These names will be listed in the immediate window in the VB Editor.
Example 4: Get the List of All Sub-folders in a Folder
The below code will give the names of all the sub-folders in the specified folder. The logic is exactly the same as covered in the above example. Instead of files, in this code, we have used sub-folders.
Example 5: Copy a File from One Place to Another
The below code will copy the file from ‘Source’ folder and copy it to the ‘Destination’ folder.
In the above code, I have used two variables – SourceFile and DestinationFolder.
Source File holds the address of the file I want to copy and the DestinationFolder variable holds the address to the folder I want the file to be copied to.
Note that it’s not sufficient to give the destination folder name when you’re copying a file. You also need to specify the file name. You can use the same file name or can also change it. In the above example, I copied the file and named it SampleFileCopy.xlsx
Example 6: Copy All Files From One Folder to Another
The below code will copy all the files from the Source folder to the destination folder.
The above code will copy all the files from the Source folder to the Destination Folder.
Note that in the MyFSO.CopyFile method, I have specified the ‘Overwritefiles’ property to be False (this is True by default). This makes sure that in case you already have the file in the folder, it’s not copied (and you will see an error). If you remove ‘Overwritefiles’ or set this to True, in case there are files in the destination folder with the same name, these would be overwritten.
Pro Tip: When copying files, there is always a chance of overwriting files. A good idea, in this case, is to add the timestamp along with the name. This will ensure that the names are always different and you can easily track which files were copied at what time.
If you want to copy the files of a certain extension only, you can do that by using an IF Then statement to check whether the extension is xlsx or not.
FileSystemObject (FSO) Methods
Here are the methods that you can use for each object. This is just for reference purpose and doesn’t worry about it too much. The usage of some of these has been shown in the examples covered above.
You May Also Like the Following Excel Tutorials:
Watch madea family reunion online free. Watching Madea's Family Reunion online free on Putlocker fast & furious presents hobbs & shaw 2019, spider man far from home 2019, crawl 2019, american horror story 1984 season 9 2019, titans season 2 2019, toy story 4 2019, it chapter two 2019, power season 6 2019, x men dark phoenix 2019, aladdin 2019, avengers endgame 2019, rambo last blood 2019. Madeas Class Reunion (2005) Full Movie, Tyler Perry's gun-toting, irreverent granny Madea, played by Perry himself, has been entertaining theater audiences with. Watch Madea's Family Reunion 4K FOR FREE Madea's Family Reunion While planning her family reunion, a pistol-packing grandma must contend with the other dramas on her plate, including the runaway who has been placed under her care, and her love-troubled nieces. Tyler Perry's gun-toting, irreverent granny Madea, played by Perry himself, has been entertaining theater audiences with plays like Diary of a Mad Black Woman and Madea's Family Reunion. This time, Madea is traveling to the Pandora Hotel, the venue for her 50-year class reunion. Madeas Family Reunion (2002) Full Movie, Just as Madea buries her sister, she must get ready for her granddaughter, Lisa, coming to get married at the house. It' M4UFree.TV.
Session expired
Please log in again. The login page will open in a new tab. After logging in you can close it and return to this page.
-->
Provides access to a computer's file system.
TotalCross (totally cross-platform) is a mobile development platform for PDA and smartphones. Using the Java language it makes possible to develop portable applications to deploy in a wide range of market smartphones without any adaptation in the source code (“write once run anywhere” concept) since TotalCross virtual machine has been installed. Rim blackberry free download - Facebook for BlackBerry, BlackBerry Desktop Software, Wikipedia for BlackBerry, and many more programs.
Syntax
Scripting.FileSystemObject
Excel Vba Microsoft Scripting Runtime ReferenceRemarksExcel Vba Microsoft Scripting Runtime Library
The following code illustrates how the FileSystemObject object is used to return a TextStream object that can be read from or written to:
In the example code:
Methods
Microsoft Scripting Runtime ReferenceProperties
See alsoSupport and feedback
Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |