Windows脚本宿主对象库

Windows脚本宿主对象库。

全名:Windows脚本宿主对象模型

LibName:IWshRuntimeScripting

位置:... \ WINDOWS \ system32 \ wshom.ocx

本技巧与ADezii先前编写的有关脚本运行时库的内容紧密相关。 这两个库都具有与文件功能相同的功能,但是本主题中的库提供了可以方便使用的其他可能性。

这里是一个简短的概述:

  • 一堆文件功能(与“脚本运行时”库中的功能相同)
  • 网络和打印机功能。
  • 创建快捷方式。
  • Shell功能包括:高级应用程序启动,事件日志和注册表功能等。

本技巧致力于两个最广泛使用的方面:创建快捷方式和启动应用程序。 那些对更全面的信息感兴趣的人应该花一些时间

MSDN:Windows脚本宿主

创建快捷方式。

我猜下面的代码很容易解释,它在“收藏夹”中创建URL快捷方式,在桌面和“开始”菜单中创建文件快捷方式。 它使用IWshRuntimeLibrary.WshShell.SpecialFolders集合来获取相关目录的路径。 可以在以下位置找到SpecialFolders集合名称的完整列表:

MSDN:SpecialFolders对象

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库提供了比VBA Shell功能更好的方法来运行不支持自动化的应用程序。

在以下示例中,当按下切换按钮时启动Calc.exe,并在释放或关闭窗体时终止。 此外,Form_Timer事件处理程序还会监视Calc.exe应用程序状态,并且每当关闭它时,都会释放切换按钮。


'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 

From: https://bytes.com/topic/access/insights/777760-windows-script-host-object-library

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值