VBA Microsoft Scripting Runtime Library
The Microsoft Scripting Runtime library contains classes that facilitate working with the file system and text files, as well as the Dictionary class.
Classes
Class | Description |
---|---|
Dictionary | Data structure which stores a duplicate-free list of data in key/item pairs. |
Drive | Represents a disk drive or network share. |
Drives | Collection of all Drive objects available on the file system. |
File | Represents a file. |
Files | Collection of File objects within a Folder object. |
FileSystemObject | Powerful object providing access to a computer's file system. |
Folder | Represents a folder. |
Folders | Collection of Folder objects within a Folder object. |
TextStream | Represents a text file. |
Enums and Constants
Compare Method
- BinaryCompare = 0
- TextCompare = 1
- DatabaseCompare = 2
DriveTypeConst
- UnknownType = 0
- Removable = 1
- Fixed = 2
- Remote = 3
- CDRom = 4
- RamDisk = 5
FileAttribute
- Normal = 0
- ReadOnly = 1
- Hidden = 2
- System = 4
- Volume = 8
- Directory = 16 (&H10)
- Archive = 32 (&H20)
- Alias = 1024 (&H400)
- Compressed = 2048 (&H800)
IOMode
- ForReading = 1
- ForWriting = 2
- ForAppending = 8
SpecialFolderConst
- WindowsFolder = 0
- SystemFolder = 1
- TemporaryFolder = 2
StandardStreamTypes
- StdIn = 0
- StdOut = 1
- StdErr = 2
Tristate
- TristateUseDefault = -2 (&HFFFFFFFE)
- TristateMixed = -2 (&HFFFFFFFE)
- TristateTrue = -1 (&HFFFFFFFF)
- TristateFalse = 0
Use Cases
The Microsoft Scripting Runtime library can be used for file system scripting, text file manipulation, and to use the Dictionary class.
Dictionary
The Dictionary class is often instrumental for programming in VBA.
Public Sub Example()
Dim Dict As Object 'Scripting.Dictionary
Set Dict = CreateObject("Scripting.Dictionary")
Dict.Add "A", 1
Dict.Add "B", 2
Dict.Add "C", 3
Debug.Print Dict("A")
End Sub
File System
The Scripting Runtime library has classes that represent the file system.
Public Sub Example()
Dim FSO As Object 'Scripting.FileSystemObject
Dim DesktopPath As String
Dim DesktopFolder As Object 'Scripting.Folder
Dim F As Object 'Scripting.File
Set FSO = CreateObject("Scripting.FileSystemObject")
DesktopPath = FSO.BuildPath(Environ$("USERPROFILE"), "Desktop")
Set DesktopFolder = FSO.GetFolder(DesktopPath)
For Each F In DesktopFolder.Files
Debug.Print F.Path
Next F
End Sub
Text Files
The Scripting Runtime library can be used to manipulate text files.
Public Sub Example()
Dim FSO As Object 'Scripting.FileSystemObject
Dim OutputFilePath As String
Dim TS As Object 'Scripting.TextStream
Set FSO = CreateObject("Scripting.FileSystemObject")
OutputFilePath = FSO.BuildPath(Environ$("USERPROFILE"), "Desktop\Test.txt")
If FSO.FileExists(OutputFilePath) Then
MsgBox OutputFilePath & " already exists."
Exit Sub
End If
Set TS = FSO.CreateTextFile(OutputFilePath)
TS.WriteLine "Hello, World!"
TS.WriteLine "1, 2, 3"
TS.WriteLine "Goodbye."
TS.Close
End Sub