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
- BinaryCompare = 0
- DatabaseCompare = 2
- TextCompare = 1
- CDRom = 4
- Fixed = 2
- RamDisk = 5
- Remote = 3
- Removable = 1
- UnknownType = 0
- Alias = 1024
- Archive = 32
- Compressed = 2048
- Directory = 16
- Hidden = 2
- Normal = 0
- ReadOnly = 1
- System = 4
- Volume = 8
- ForAppending = 8
- ForReading = 1
- ForWriting = 2
- SystemFolder = 1
- TemporaryFolder = 2
- WindowsFolder = 0
- StdErr = 2
- StdIn = 0
- StdOut = 1
- TristateFalse = 0
- TristateMixed = -2
- TristateTrue = -1
- TristateUseDefault = -2
- WshFailed = 2
- WshFinished = 1
- WshRunning = 0
- WshHide = 0
- WshMaximizedFocus = 3
- WshMinimizedFocus = 2
- WshMinimizedNoFocus = 6
- WshNormalFocus = 1
- 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