Automate an application using VBScript

2020-07-20 00:07发布

I am new to VB Scripting. I want the VB script to act according to an event of an application. Eg : The VBScript should wait till a popup message appears in an application.

标签: vbscript
3条回答
smile是对你的礼貌
2楼-- · 2020-07-20 00:14

If all you want is to close a pop-up, this may help you:

Set oShell = CreateObject("WScript.Shell") 

Do 
    bResult = oShell.AppActivate("Title of the dialog box") 
    If bResult = True Then 
        oShell.SendKeys "%N" ' Alt+N, you may send {Esc} or {Enter} if you want
        Exit Do 
    End If 
    WScript.Sleep 500 
Loop 
查看更多
趁早两清
3楼-- · 2020-07-20 00:35

VBScript and Windows Script Host only support primitive GUI automation, like activating windows (the AppActivate method) and sending keystrokes (the SendKeys methods). So I doubt that your task can be accomplished with pure VBScript.

However, there's a free GUI automation tool called AutoIt that should be able to do what you need.

查看更多
劳资没心,怎么记你
4楼-- · 2020-07-20 00:36

Depending on the application, the object model can be used to automate most functions. I routinely automate Excel using the Excel Object Model. Here is a link to the documentation at MSDN:

http://msdn.microsoft.com/en-us/library/aa209782%28office.10%29.aspx

I usually turn off the pop-up menus when possible to prevent them from causing unexpected events for the script to handle. Here is an example:

'Create an new instance of Excel to work with
Set objExcel = CreateObject("Excel.Application")
Set objBook = objExcel.Workbooks.Open("C:\Test.xls")
Set objSheet = objStartsBook.Sheets(1)
'Make Excel visible on the screen for the user
objExcel.Visible = True
'Turn off pop up dialog boxes from excel so the script can control the app
objExcel.DisplayAlerts = False

From here, I would continue to used the object model for Excel to get the application to do whatever task I need.

The trick is thinking about what members and methods are needed to accomplish the task you want versus how a person interacts with the GUI.

查看更多
登录 后发表回答