VBA Windows Script Host Runtime Library

The Windows Script Host Runtime library provides scripting capabilities.

Classes

Member Description
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.
WshCollection Returned by the WshNetwork.EnumNetworkDrives method and the WshNetwork.EnumPrinterConnections method. A collection that associates pairs of network drive/printer local names and their associated UNC names. Even-numbered items in the collection represent local names of drive/printer ports and odd-numbered items represent UNC names. The first item in the collection is at index zero.
WshEnvironment Provides access to Windows environment variables.
WshExec Returned by WshShell.Exec method. Provides status and error info about a script.
WshNetwork Provides access to network resources.
WshShell Provides access to the Windows shell.
WshShortcut Allows a shortcut to be created programmatically.
WshURLShortcut Allows a shortcut to an Internet resource to be created programmatically.

Enums and Consts

CompareMethod
  1. BinaryCompare = 0
  2. DatabaseCompare = 2
  3. TextCompare = 1
DriveTypeConst
  1. CDRom = 4
  2. Fixed = 2
  3. RamDisk = 5
  4. Remote = 3
  5. Removable = 1
  6. UnknownType = 0
FileAttribute
  1. Alias = 1024
  2. Archive = 32
  3. Compressed = 2048
  4. Directory = 16
  5. Hidden = 2
  6. Normal = 0
  7. ReadOnly = 1
  8. System = 4
  9. Volume = 8
IOMode
  1. ForAppending = 8
  2. ForReading = 1
  3. ForWriting = 2
SpecialFolderConst
  1. SystemFolder = 1
  2. TemporaryFolder = 2
  3. WindowsFolder = 0
StandardStreamTypes
  1. StdErr = 2
  2. StdIn = 0
  3. StdOut = 1
Tristate
  1. TristateFalse = 0
  2. TristateMixed = -2
  3. TristateTrue = -1
  4. TristateUseDefault = -2
WshExecStatus
  1. WshFailed = 2
  2. WshFinished = 1
  3. WshRunning = 0
WshWindowStyle
  1. WshHide = 0
  2. WshMaximizedFocus = 3
  3. WshMinimizedFocus = 2
  4. WshMinimizedNoFocus = 6
  5. WshNormalFocus = 1
  6. WshNormalNoFocus = 4

Use Cases

Some use cases for the IWshRuntimeLibrary library are creating shortcuts, executing shell commands, accessing the file system, carrying out administrative tasks or network tasks, and interacting with the Windows Registry.

Create Shortcuts

Shortcuts can be created to a file, application, or a webpage.

Public Sub Example()

    '''Shortcut to file/application

    Dim SH As Object 'IWshRuntimeLibrary.WshShell
    Set SH = CreateObject("WScript.Shell")

    Dim PathLink As String
    PathLink = SH.SpecialFolders("Desktop") & "\Example Application Shortcut.lnk"

    Dim SC As Object 'IWshRuntimeLibrary.WshShortcut
    Set SC = SH.CreateShortcut(PathLink)

    SC.TargetPath = "C:\Windows\notepad.exe"
    SC.Save

End Sub
Public Sub Example()

    '''Shortcut to webpage

    Dim SH As Object 'IWshRuntimeLibrary.WshShell
    Set SH = CreateObject("WScript.Shell")

    Dim PathLink As String
    PathLink = SH.SpecialFolders("Desktop") & "\Example Webpage Shortcut.url"

    Dim SC As Object 'IWshRuntimeLibrary.WshURLShortcut
    Set SC = SH.CreateShortcut(PathLink)

    SC.TargetPath = "https://www.bing.com/"
    SC.Save

End Sub

Execute Commands

Commands can be executed and the output can be returned through a TextStream. Consider a batch file "C:\Example.bat" which calls the "Dir" command.


@echo off
dir
Public Sub Example()

    Dim SH  As Object 'IWshRuntimeLibrary.WshShell
    Dim E   As Object 'IWshRuntimeLibrary.WshExec
    Dim TS  As Object 'IWshRuntimeLibrary.TextStream
    Dim S   As String

    Set SH = CreateObject("WScript.Shell")

    SH.CurrentDirectory = SH.SpecialFolders("Desktop")

    Set E = SH.Exec("C:\Example.bat")

    If E.ExitCode = 0 Then
        Set TS = E.StdOut
        S = TS.ReadAll()
        Debug.Print S
    Else
        Debug.Print E.ExitCode
    End If

End Sub

Access The File System

The file system can be accessed via classes which represent parts of the file system.

Public Sub Example()

    Dim SH As Object 'IWshRuntimeLibrary.WshShell
    Set SH = CreateObject("WScript.Shell")

    Dim FSO As Object 'IWshRuntimeLibrary.FileSystemObject
    Set FSO = CreateObject("Scripting.FileSystemObject")

    Dim FD As Object 'IWshRuntimeLibrary.Folder
    Set FD = FSO.GetFolder(SH.SpecialFolders("Desktop"))

    RecurseFolder FD
  
End Sub

Private Sub RecurseFolder(FD As Object) 'IWshRuntimeLibrary.Folder)

    Debug.Print FD.Path

    'Dim F As Object 'IWshRuntimeLibrary.File
    'For Each F In FD.Files
    '    Debug.Print F.Name
    'Next F

    Dim SubFD As Object 'IWshRuntimeLibrary.Folder

    For Each SubFD In FD.SubFolders
        RecurseFolder SubFD
    Next SubFD

End Sub

Network Tasks

The IWshRuntimeLibrary library can be used to interact with network drives and printers.

Public Sub Example()

    Dim i As Long

    Dim NW As Object 'IWshRuntimeLibrary.WshNetwork
    Set NW = CreateObject("WScript.Network")

    Dim Ds As Object 'IWshRuntimeLibrary.WshCollection
    Set Ds = NW.EnumNetworkDrives
    Debug.Print "Network drive mappings:"
    For i = 0 To Ds.Count - 1 Step 2
        Debug.Print "Drive: " & Ds.Item(i) & " | " & Ds.Item(i + 1)
    Next

    Dim Ps As Object 'IWshRuntimeLibrary.WshCollection
    Set Ps = NW.EnumPrinterConnections
    Debug.Print "Network printer mappings:"
    For i = 0 To Ps.Count - 1 Step 2
        Debug.Print "Port " & Ps.Item(i) & " | " & Ps.Item(i + 1)
    Next

End Sub

Windows Registry

The IWshRuntimeLibrary library can be used to read, write, and delete keys in the Windows Registry.

Public Sub Example()

    Dim SH As Object 'IWshRuntimeLibrary.WshShell
    Set SH = CreateObject("WScript.Shell")

    Dim Result As Variant
    Result = SH.RegRead("HKEY_CLASSES_ROOT\WScript.Shell\CurVer\")

    Debug.Print Result

End Sub