Excel respond to a web DOM event via VBA

Snypa

New Member
Joined
Nov 1, 2013
Messages
45
I have an Excel document. It creates an Internet Explorer instance and navigates to a URL.

*I am testing this with duckduckgo*

I want to be able to respond, within vba, to a button being clicked on the website.

`html.querySelector("#search_button_homepage")` finds the input element, how can I detect the onclick event and run some vba code?

My goal is to fill some cells in the spreadsheet with the data the user filled in on the web form. I can grab the data, I just need to know when the user is done (i.e. they clicked submit)


VBA Code:
Private Sub cmdTest_Click()

    Dim IE As InternetExplorer, html As Object
    Set IE = New InternetExplorer

    Dim link As String
    link = "http://www.duckduckgo.com/" 
    
    With IE
        .Visible = True
        .navigate link
        While .Busy = True Or .readyState < 4
            DoEvents
        Wend
        
        Set html = .document
              
        html.querySelector("#search_button_homepage")

        ' Respond to html.querySelector("#search_button_homepage") being clicked.
 
    End With
ShowWindow IE.hwnd, SW_SHOWMAXIMIZED
End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
One way is to simply wait until IE is displaying the page after the user's button click:
VBA Code:
    Do
        DoEvents
        Sleep 500
    Loop Until InStr(1, html.body.innerText, "some text in the page", vbTextCompare) > 0
Similarly you could wait for the IE.LocationURL or IE.LocationName until it contains some string.

Put this at the top of the module for the Sleep function:
VBA Code:
#If VBA7 Then
    Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
#Else
    Private Declare Sub Sleep Lib "kernel32" (ByVal milliseconds As Long)
#End If
 
Upvote 0

Forum statistics

Threads
1,213,562
Messages
6,114,322
Members
448,564
Latest member
ED38

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top