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
  1. BinaryCompare = 0
  2. TextCompare = 1
  3. DatabaseCompare = 2
DriveTypeConst
  1. UnknownType = 0
  2. Removable = 1
  3. Fixed = 2
  4. Remote = 3
  5. CDRom = 4
  6. RamDisk = 5
FileAttribute
  1. Normal = 0
  2. ReadOnly = 1
  3. Hidden = 2
  4. System = 4
  5. Volume = 8
  6. Directory = 16 (&H10)
  7. Archive = 32 (&H20)
  8. Alias = 1024 (&H400)
  9. Compressed = 2048 (&H800)
IOMode
  1. ForReading = 1
  2. ForWriting = 2
  3. ForAppending = 8
SpecialFolderConst
  1. WindowsFolder = 0
  2. SystemFolder = 1
  3. TemporaryFolder = 2
StandardStreamTypes
  1. StdIn = 0
  2. StdOut = 1
  3. StdErr = 2
Tristate
  1. TristateUseDefault = -2 (&HFFFFFFFE)
  2. TristateMixed = -2 (&HFFFFFFFE)
  3. TristateTrue = -1 (&HFFFFFFFF)
  4. 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