By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,316 Members | 2,132 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

Windows Script Host Object library

FishVal
Expert 2.5K+
P: 2,653
Windows Script Host Object library.

Full name: Windows Script Host Object Model
LibName: IWshRuntimeScripting
Location: ...\WINDOWS\system32\wshom.ocx

The present tip is closely related to the previous stuff written by ADezii concerning Scripting Runtime library. Both libraries share the same functionality as for file functions but the library in the topic provides additional possibilities which may be advantageously used.

Here is a brief overview:
  • A whole bunch of file functions (the same as in Scripting Runtime library)
  • Network and printer functions.
  • Creating shortcuts.
  • Shell functions including: advanced application launch, event log and registry functions etc.

The present tip is devoted to two most widely used aspects: creating shortcuts and application launch. Those interested in more comprehensive information should take a time to go through MSDN: Windows Script Host.


Creating shortcuts.

The code below, quite self-explaining I guess, creates URL shortcut in "Favorites" and file shortcuts on Desktop and in Start Menu. It uses IWshRuntimeLibrary.WshShell.SpecialFolders collection to obtain paths to a relevant directories. The full list of the names of SpecialFolders collection one could be found in MSDN: SpecialFolders object.
Expand|Select|Wrap|Line Numbers
  1. Public Sub CreateShortcuts()
  2.  
  3.     'variables declaration
  4.     Dim objWshShell As New IWshRuntimeLibrary.WshShell
  5.     Dim objWshShortcut As IWshRuntimeLibrary.WshShortcut
  6.     Dim objWshURLShortcut As IWshRuntimeLibrary.WshURLShortcut
  7.     Dim strTargetFolder As String
  8.  
  9.     'create shortcut in "Favorites"
  10.     With objWshShell
  11.         'get path to "Favorites"
  12.         strTargetFolder = .SpecialFolders("Favorites")
  13.         'create an URL shortcut in "Favorites"
  14.         Set objWshURLShortcut = .CreateShortcut(strTargetFolder & _
  15.             "\TheScripts.url")
  16.     End With
  17.     'set the URL shortcut taget and save it
  18.     With objWshURLShortcut
  19.         .TargetPath = "http://www.thescripts.com"
  20.         .Save
  21.     End With
  22.  
  23.     'create shortcut on Desktop available for all users
  24.     With objWshShell
  25.         'get path to "All Users Desktop"
  26.         strTargetFolder = .SpecialFolders("AllUsersDeskTop")
  27.         'create a shortcut in "All Users Desktop"
  28.         Set objWshShortcut = .CreateShortcut(strTargetFolder & _
  29.             "\MyShortcut.lnk")
  30.     End With
  31.     With objWshShortcut
  32.         'set the shortcut target, description and save it
  33.         .TargetPath = "C:\SomeFile.txt"
  34.         .Description = "Link to my file"
  35.         .Save
  36.     End With
  37.  
  38.     'create the same shortcut in Start Menu available for all users
  39.     With objWshShell
  40.         strTargetFolder = .SpecialFolders("AllUsersStartMenu")
  41.         Set objWshShortcut = .CreateShortcut(strTargetFolder & _
  42.             "\MyShortcut.lnk")
  43.     End With
  44.     With objWshShortcut
  45.         .TargetPath = "C:\SomeFile.txt"
  46.         .Description = "Link to my file"
  47.         .Save
  48.     End With
  49.  
  50.     'destroy object variables
  51.     Set objWshURLShortcut = Nothing
  52.     Set objWshShortcut = Nothing
  53.     Set objWshShell = Nothing
  54.  
  55. End Sub
  56.  
Launch an application.

WSHOM library provides a better then VBA Shell function method to run an application which doesn't support automation.
In the following example Calc.exe is launched when a toggle button is pressed and terminated when it is released or form is closed. Additionally Form_Timer event handler monitors Calc.exe application status and whenever it is closed the toggle button is being released.
Expand|Select|Wrap|Line Numbers
  1. 'form public variable to store WshExec object referring
  2. 'to launched Calc.exe application
  3. Dim objCalc As IWshRuntimeLibrary.WshExec
  4.  
  5. Private Sub Form_Close()
  6.  
  7.     'terminate Calc.exe when form is being closed
  8.     If Not objCalc Is Nothing Then
  9.         objCalc.Terminate
  10.         Set objCalc = Nothing
  11.     End If
  12.  
  13. End Sub
  14.  
  15. 'form timer event handler to monitor Calc.exe application status
  16. Private Sub Form_Timer()
  17.  
  18.     'if public WshExec variable does not exist then leave procedure
  19.     If objCalc Is Nothing Then Exit Sub
  20.     'set toggle button state according to Calc.exe status
  21.     With Me
  22.         If objCalc.Status = WshRunning Then
  23.             .tglCalc = True
  24.         Else
  25.             .tglCalc = False
  26.         End If
  27.     End With
  28.  
  29. End Sub
  30.  
  31. Private Sub tglCalc_AfterUpdate()
  32.  
  33.     Dim objWshShell As New IWshRuntimeLibrary.wshShell
  34.  
  35.     If Me.tglCalc Then
  36.         'if toggle button is pressed then run Calc.exe
  37.         'and assign returned WshExec object to form public variable
  38.         Set objCalc = objWshShell.Exec("calc.exe")
  39.     Else
  40.         'if toggle button is released then terminate Calc.exe
  41.         objCalc.Terminate
  42.     End If
  43.  
  44.     Set objWshShell = Nothing
  45.  
  46. End Sub
  47.  
Mar 3 '08 #1
Share this Article
Share on Google+
3 Comments


dima69
Expert 100+
P: 181
My concern is (as usual :-)) how robust this solution is. Personally, I don't like using OCX, as they often make trouble with installation because of the version compatibility.
Can this WshShell object be used via late binding ? (i.e., by CreateObject) ?
Mar 4 '08 #2

FishVal
Expert 2.5K+
P: 2,653
There are no 100% robust solutions at all. ;)
And, you are right, WSHO objects could not be instantiated via CreateObject.
Mar 4 '08 #3

ADezii
Expert 5K+
P: 8,638
Nice job, FishVal! Any chance of a sequel demonstrating the Printing and Networking functionality of the Windows Script Host Object Library?
Mar 8 '08 #4