Windows Script Host Object library.
Full name: Windows Script Host Object Model
LibName: IWshRuntimeScri pting
Location: ...\WINDOWS\sys tem32\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:
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 IWshRuntimeLibr ary.WshShell.Sp ecialFolders 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.
[code=vb]
Public Sub CreateShortcuts ()
'variables declaration
Dim objWshShell As New IWshRuntimeLibr ary.WshShell
Dim objWshShortcut As IWshRuntimeLibr ary.WshShortcut
Dim objWshURLShortc ut As IWshRuntimeLibr ary.WshURLShort cut
Dim strTargetFolder As String
'create shortcut in "Favorites"
With objWshShell
'get path to "Favorites"
strTargetFolder = .SpecialFolders ("Favorites" )
'create an URL shortcut in "Favorites"
Set objWshURLShortc ut = .CreateShortcut (strTargetFolde r & _
"\TheScripts.ur l")
End With
'set the URL shortcut taget and save it
With objWshURLShortc ut
.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 ("AllUsersDeskT op")
'create a shortcut in "All Users Desktop"
Set objWshShortcut = .CreateShortcut (strTargetFolde r & _
"\MyShortcut.ln k")
End With
With objWshShortcut
'set the shortcut target, description and save it
.TargetPath = "C:\SomeFile.tx t"
.Description = "Link to my file"
.Save
End With
'create the same shortcut in Start Menu available for all users
With objWshShell
strTargetFolder = .SpecialFolders ("AllUsersStart Menu")
Set objWshShortcut = .CreateShortcut (strTargetFolde r & _
"\MyShortcut.ln k")
End With
With objWshShortcut
.TargetPath = "C:\SomeFile.tx t"
.Description = "Link to my file"
.Save
End With
'destroy object variables
Set objWshURLShortc ut = Nothing
Set objWshShortcut = Nothing
Set objWshShell = Nothing
End Sub
[/code]
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.
[code=vb]
'form public variable to store WshExec object referring
'to launched Calc.exe application
Dim objCalc As IWshRuntimeLibr ary.WshExec
Private Sub Form_Close()
'terminate Calc.exe when form is being closed
If Not objCalc Is Nothing Then
objCalc.Termina te
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_AfterUp date()
Dim objWshShell As New IWshRuntimeLibr ary.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.Exe c("calc.exe")
Else
'if toggle button is released then terminate Calc.exe
objCalc.Termina te
End If
Set objWshShell = Nothing
End Sub
[/code]
Full name: Windows Script Host Object Model
LibName: IWshRuntimeScri pting
Location: ...\WINDOWS\sys tem32\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 IWshRuntimeLibr ary.WshShell.Sp ecialFolders 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.
[code=vb]
Public Sub CreateShortcuts ()
'variables declaration
Dim objWshShell As New IWshRuntimeLibr ary.WshShell
Dim objWshShortcut As IWshRuntimeLibr ary.WshShortcut
Dim objWshURLShortc ut As IWshRuntimeLibr ary.WshURLShort cut
Dim strTargetFolder As String
'create shortcut in "Favorites"
With objWshShell
'get path to "Favorites"
strTargetFolder = .SpecialFolders ("Favorites" )
'create an URL shortcut in "Favorites"
Set objWshURLShortc ut = .CreateShortcut (strTargetFolde r & _
"\TheScripts.ur l")
End With
'set the URL shortcut taget and save it
With objWshURLShortc ut
.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 ("AllUsersDeskT op")
'create a shortcut in "All Users Desktop"
Set objWshShortcut = .CreateShortcut (strTargetFolde r & _
"\MyShortcut.ln k")
End With
With objWshShortcut
'set the shortcut target, description and save it
.TargetPath = "C:\SomeFile.tx t"
.Description = "Link to my file"
.Save
End With
'create the same shortcut in Start Menu available for all users
With objWshShell
strTargetFolder = .SpecialFolders ("AllUsersStart Menu")
Set objWshShortcut = .CreateShortcut (strTargetFolde r & _
"\MyShortcut.ln k")
End With
With objWshShortcut
.TargetPath = "C:\SomeFile.tx t"
.Description = "Link to my file"
.Save
End With
'destroy object variables
Set objWshURLShortc ut = Nothing
Set objWshShortcut = Nothing
Set objWshShell = Nothing
End Sub
[/code]
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.
[code=vb]
'form public variable to store WshExec object referring
'to launched Calc.exe application
Dim objCalc As IWshRuntimeLibr ary.WshExec
Private Sub Form_Close()
'terminate Calc.exe when form is being closed
If Not objCalc Is Nothing Then
objCalc.Termina te
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_AfterUp date()
Dim objWshShell As New IWshRuntimeLibr ary.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.Exe c("calc.exe")
Else
'if toggle button is released then terminate Calc.exe
objCalc.Termina te
End If
Set objWshShell = Nothing
End Sub
[/code]
Comment