468,727 Members | 1,619 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

Windows Script Host Object library

2,653 Expert 2GB
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()
  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
  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
  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
  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
  50.     'destroy object variables
  51.     Set objWshURLShortcut = Nothing
  52.     Set objWshShortcut = Nothing
  53.     Set objWshShell = Nothing
  55. End Sub
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
  5. Private Sub Form_Close()
  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
  13. End Sub
  15. 'form timer event handler to monitor Calc.exe application status
  16. Private Sub Form_Timer()
  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
  29. End Sub
  31. Private Sub tglCalc_AfterUpdate()
  33.     Dim objWshShell As New IWshRuntimeLibrary.wshShell
  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
  44.     Set objWshShell = Nothing
  46. End Sub
Mar 3 '08 #1
3 16171
181 Expert 100+
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
2,653 Expert 2GB
There are no 100% robust solutions at all. ;)
And, you are right, WSHO objects could not be instantiated via CreateObject.
Mar 4 '08 #3
8,800 Expert 8TB
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

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by raghavendra | last post: by
7 posts views Thread by IntraRELY | last post: by
2 posts views Thread by Miky | last post: by
3 posts views Thread by Angus Comber | last post: by
69 posts views Thread by Peter Olcott | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.