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
- Public Sub CreateShortcuts()
- 'variables declaration
- Dim objWshShell As New IWshRuntimeLibrary.WshShell
- Dim objWshShortcut As IWshRuntimeLibrary.WshShortcut
- Dim objWshURLShortcut As IWshRuntimeLibrary.WshURLShortcut
- Dim strTargetFolder As String
- 'create shortcut in "Favorites"
- With objWshShell
- 'get path to "Favorites"
- strTargetFolder = .SpecialFolders("Favorites")
- 'create an URL shortcut in "Favorites"
- Set objWshURLShortcut = .CreateShortcut(strTargetFolder & _
- "\TheScripts.url")
- End With
- 'set the URL shortcut taget and save it
- With objWshURLShortcut
- .TargetPath = "http://www.thescripts.com"
- .Save
- End With
- 'create shortcut on Desktop available for all users
- With objWshShell
- 'get path to "All Users Desktop"
- strTargetFolder = .SpecialFolders("AllUsersDeskTop")
- 'create a shortcut in "All Users Desktop"
- Set objWshShortcut = .CreateShortcut(strTargetFolder & _
- "\MyShortcut.lnk")
- End With
- With objWshShortcut
- 'set the shortcut target, description and save it
- .TargetPath = "C:\SomeFile.txt"
- .Description = "Link to my file"
- .Save
- End With
- 'create the same shortcut in Start Menu available for all users
- With objWshShell
- strTargetFolder = .SpecialFolders("AllUsersStartMenu")
- Set objWshShortcut = .CreateShortcut(strTargetFolder & _
- "\MyShortcut.lnk")
- End With
- With objWshShortcut
- .TargetPath = "C:\SomeFile.txt"
- .Description = "Link to my file"
- .Save
- End With
- 'destroy object variables
- Set objWshURLShortcut = Nothing
- Set objWshShortcut = Nothing
- Set objWshShell = Nothing
- End Sub
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
- 'form public variable to store WshExec object referring
- 'to launched Calc.exe application
- Dim objCalc As IWshRuntimeLibrary.WshExec
- Private Sub Form_Close()
- 'terminate Calc.exe when form is being closed
- If Not objCalc Is Nothing Then
- objCalc.Terminate
- Set objCalc = Nothing
- End If
- End Sub
- 'form timer event handler to monitor Calc.exe application status
- Private Sub Form_Timer()
- 'if public WshExec variable does not exist then leave procedure
- If objCalc Is Nothing Then Exit Sub
- 'set toggle button state according to Calc.exe status
- With Me
- If objCalc.Status = WshRunning Then
- .tglCalc = True
- Else
- .tglCalc = False
- End If
- End With
- End Sub
- Private Sub tglCalc_AfterUpdate()
- Dim objWshShell As New IWshRuntimeLibrary.wshShell
- If Me.tglCalc Then
- 'if toggle button is pressed then run Calc.exe
- 'and assign returned WshExec object to form public variable
- Set objCalc = objWshShell.Exec("calc.exe")
- Else
- 'if toggle button is released then terminate Calc.exe
- objCalc.Terminate
- End If
- Set objWshShell = Nothing
- End Sub