Catch an Event in VBScript

guys. This is a VBS script that opens Google, fills out a form and clicks the search button.

set ie = CreateObject("InternetExplorer.Application")

ie.navigate("www.google.com")

ie.visible = true

while ie.readystate <> 4
    wscript.sleep 100
WEnd

set fields = ie.document.getelementsbyname("q")
set buttons = ie.document.getelementsbyname("btnG")

fields(0).value = "some query"
buttons(0).click

ie.quit

Sub OnClickSub()
    MsgBox  "button clicked!", 0
End Sub

Obviously, it buttons(0).clickfires the onclick event of the button, which I somehow need to catch in my script, and provide it with some processing, such as launching OnClickSub().

Anyone have any ideas how this should be done?

+3
source share
2 answers

Use the function GetRefto get a pointer to an event handler and bind it to onclick, for example:

buttons(0).onclick = GetRef("OnClickSub")

(Apparently attachEventnot working when called from outside the webpage.)

+3
source

, Excel. Excel , . VBScript . Excel . , , .

'/////////////////////////////////////////////////Event Constant Definitions/////////////////////////////////////////////////////
    EVENT_CELL_CHANGE = 1   '//////////////////////////////////////////////////////////////////////
    EVENT_SELECTED_CELL_CHANGE = 2 '/////////////////////////////////////////////////////
    EVENT_SELECTED_SHEET_CHANGE = 3 '///////////////////////////////////////////////////
    EVENT_SELECTED_WORKBOOK_CHANGE = 4 '///////////////////////////////////////
'/////////////////////////////////////////////////Event Constant Definitions/////////////////////////////////////////////////////
'_______________________________________________________________________
'/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
'////////////////////////////////////////////////////Global Program/////////////////////////////////////////////////////////////////
'/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
set O = getObject( , "Excel.Application")
Call WaitUntil_Event(EVENT_CELL_CHANGE, O.Selection, RtnVar, "BLANK")
msgbox RtnVar.address

'/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
'////////////////////////////////////////////End of Global Program//////////////////////////////////////////////////////////////
'/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
'_______________________________________________________________________
'/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
'///////////////////////////////////////////Start of Main Sub Program/////////////////////////////////////////////////////////
'/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
'///////////////////////////////////////////This program was written by///////////////////////////////////////////////////////
'///////////////////////////////////////////Ben Ahrens (April 5th, 2019///////////////////////////////////////////////////////
'/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
'/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
'/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Sub WaitUntil_Event(EventType, CheckVal, ByRef ReturnVariable, RunVBScript_FileName)
    On Error Resume Next
    LoopedCycleTime = 50    
    WaitUntil_SelectionChange = False
    'msgbox EVENT_CELL_CHANGE 
    select case EventType
        Case EVENT_CELL_CHANGE
            Set CheckVal = O.Selection
            Do
                Set CheckVal = O.Selection
                Val= CheckVal.Value
                Do 
                    WScript.Sleep LoopedCycleTime
                Loop Until O.Selection.Address <> CheckVal.Address
            Loop While CheckVal.Value = Val
            Set RtnVar = CheckVal
        Case EVENT_SELECTED_CELL_CHANGE 
            Do 
                WScript.Sleep LoopedCycleTime
            Loop Until O.Selection.Address <> CheckVal.Address
        Case EVENT_SELECTED_SHEET_CHANGE 
            Do 
                WScript.Sleep LoopedCycleTime
            Loop Until O.Selection.Worksheet.Name <> CheckVal.Worksheet.Name

        Case EVENT_SELECTED_WORKBOOK_CHANGE 
            Do 
                WScript.Sleep LoopedCycleTime
            Loop Until O.Selection.Worksheet.Parent.Name <> CheckVal.Worksheet.Parent.Name
    End Select
    'WaitUntil_Event = True
    'Include RunVBScript_FileName

End Sub

'/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
'///////////////////////////////////////////////End of Main Sub Program//////////////////////////////////////////////////////
'/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
0

Source: https://habr.com/ru/post/1737713/


All Articles