Shell Scripting using VBA and the Windows Script Host Object Model

WSHOM
image credit: Windows Script Host Object Model

The Windows Script Host Object Model (WSHOM) is a rich library that allows us to write scripts for controlling Windows. It also provides access to environment variables and a few other things we'll explore in this article. I won't review all the properties and methods of WSHOM here; for that, visit Windows Script Host Object Model.

To write the code for these examples, I set a reference to the Windows Script Host Object Model. The file is located in your system32 folder, filename wshom.ocx. To reference it, go to Tools > References in the VB IDE and scroll until you see "Windows Script Host Object Model." If you don't see it, click Browse….

You don't have to do this, but you won't get any Intellisense when exploring the properties and methods of this object model. After writing the code, you can always convert the references to be declared As Object and de-reference the Windows Script Host Object Model (an example of this technique may be found at Take advantage of Intellisense when writing late bound code) to make the code late-bound.

WshShell Object

From MSDN: Gives you access to the native Windows shell functionality.

To get a reference to the WshShell object, use the following code:

Dim ws As WshShell  ' As Object if late-bound
Set ws = CreateObject("Wscript.Shell")
' if early bound, you can also use = New WshShell

WshShell Properties

  • CurrentDirectory
  • The CurrentDirectory property of the WshShell object returns the current directory as a string. (Note that the CurDir function does the same thing, without the overhead of the WshShell object.)

    Dim ws As WshShell  ' As Object if late-bound
    Set ws = CreateObject("Wscript.Shell")
    ' if early bound, you can also use = New WshShell
    Debug.Print ws.CurrentDirectory
    ' CurDir Function
    Debug.Print CurDir
  • Environment
  • The Environment property is a collection of values that hold information about the system environment. (Note that the Environ function provides similar information.)

    Dim ws As WshShell  ' As Object if late-bound
    Dim wshenv As WshEnvironment  ' As Object if late-bound
    Set ws = CreateObject("Wscript.Shell")
    ' if early bound, you can also use = New WshShell
    Set wshenv = ws.Environment("System")
    
    Debug.Print wshenv("OS")
    ' Environ Function
    Debug.Print Environ("OS")

    For a full listing of environment properties you can read, see Environment Property on MSDN.

  • SpecialFolders
  • The SpecialFolders property returns a collection of special folders. Special folders are system folders (String variables) like your Desktop folder, the Startup folder, the Favorites folder.

    Dim ws As WshShell  ' As Object if late-bound
    Dim wshSF As Object ' there's no As SpecialFolders ?
    Set ws = CreateObject("Wscript.Shell")
    ' if early bound, you can also use = New WshShell
    Set wshSF = ws.SpecialFolders
    Debug.Print ws.SpecialFolders("Desktop")
    
    ' randomly print a few special folder paths
    Debug.Print wshSF.Item(1)
    Debug.Print wshSF.Item(2)
    Debug.Print wshSF.Item(3)
    Debug.Print wshSF.Item(4)
    Debug.Print wshSF.Item(5)

    For a complete listing of SpecialFolders you can read, visit the SpecialFolders Property on MSDN.

WshShell Methods

  • CreateShortcut
  • You can create shortcuts to a file or folder using this method. The MSDN article on CreateShortcut has sample code explaining how to do this.

  • Popup
  • Yes, you can even use WshShell to show popups. MsgBox works well for this, without the overhead, but for scripts, you'll need this method. It can be as simple as

    Dim ws As WshShell  ' As Object if late-bound
    Set ws = CreateObject("Wscript.Shell")
    ' if early bound, you can also use = New WshShell
    ws.Popup ("Hello World")
    
  • Registry Manipulation
  • The WshShell provides three methods for working with the registry: RegDelete, RegRead and RegWrite. You can delete, read and write any key or value in the Registry. Use with caution!

    For code sample, visit Check Your Premises.

  • Run
  • The Run method will start a given program. The best use I've found is to open any program natively in its own application. For a code sample, see Open Any Email Attachment From Outlook.

WshNetwork Object

From MSDN: Provides access to the shared resources on the network to which your computer is connected.

To get a reference to the WshNetwork object, use the following code:

Dim ws As WshNetwork  ' As Object if late-bound
Set ws = CreateObject("Wscript.Network")
' if early bound, you can also use = New WshNetwork

WshNetwork Properties

  • ComputerName
  • Returns the name of the computer (found by right-clicking My Computer, choose Properties, go to Computer Name tab).

    Dim ws As WshNetwork  ' As Object if late-bound
    Set ws = CreateObject("Wscript.Network")
    Debug.Print ws.ComputerName
  • UserDomain
  • Returns the domain name for the current user (found by right-clicking My Computer, choose Properties, go to Computer Name tab).

    Dim ws As WshNetwork  ' As Object if late-bound
    Set ws = CreateObject("Wscript.Network")
    Debug.Print ws.UserDomain
  • UserName
  • Returns the name of the current user.

    Dim ws As WshNetwork  ' As Object if late-bound
    Set ws = CreateObject("Wscript.Network")
    Debug.Print ws.UserName

WshNetwork Methods

  • EnumNetworkDrives
  • EnumPrinterConnections
  • MSDN has great sample code for enumerating both of these collections, which (respectively) hold the list of network drives and printer connections, which you can use in the two methods below.

  • MapNetworkDrive
  • SetDefaultPrinter
  • Both of these methods require the UNC name of the network path or printer.

Site last updated: August 20, 2014

Peltier Tech Charting Utilities for Excel