0

I use SHDocVw.InternetExplorer to show in powerpoint a local html file to the user. The file contains a form with a submit button and I would like to run my vba function when the button is clicked. I did the following to handle the onQuit-event of the InternetExplorer.

Class module "CIE"

Public WithEvents ie As SHDocVw.InternetExplorer

Private Sub ie_onQuit()
    Debug.Print "onQuit was fired"
End Sub

Module code

Option Explicit

Public curCIE As CIE

Sub open_ie()
    Dim CIEobj As CIE
    Set CIEobj = New CIE

    Dim ie As SHDocVw.InternetExplorer
    Set ie = New SHDocVw.InternetExplorer

    ie.Visible = True
    ie.Navigate "C:\search.html"

    Do While ie.Busy Or ie.readyState <> 4
        DoEvents
    Loop

    Set CIEobj.ie = ie
    Set curCIE = CIEobj
End Sub

I actually want to handle the onclick event of

curCIE.ie.Document.getElementsByTagName("button").Item(0)

but I do not know how to proceed.

  • Declare some `App` variable within IE `window` object via `.execScript` method, assign it the reference to the Excel `Application` object, then call Excel macro with `App.Run "MacroName"` within IE button onclick event handler code. [This example](https://stackoverflow.com/a/38134477/2165759) may help. – omegastripes Aug 02 '17 at 22:16

1 Answers1

2

You could add reference to MSHTML, then make your class module this way:

Public WithEvents ie As SHDocVw.InternetExplorer

Private WithEvents button As MSHTML.HTMLButtonElement

Public Sub LoadButton()
    Set button = ie.document.getElementsByTagName("button").Item(0)
End Sub

Private Function button_onclick() As Boolean
    'do what you want here
End Function

Private Sub ie_onQuit()
    Debug.Print "onQuit was fired"
End Sub

And then, in the other module, add this statement after setting CIEobj.ie property:

CIEobj.LoadButton
VBobCat
  • 2,527
  • 4
  • 29
  • 56