Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save muhdkhokhar/47d42812343bb0d25797770fb471fefb to your computer and use it in GitHub Desktop.
Save muhdkhokhar/47d42812343bb0d25797770fb471fefb to your computer and use it in GitHub Desktop.
Sub RunJavaScriptUsingIE()
On Error GoTo ErrorHandler
' Create Internet Explorer object
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
' Create an HTML page with JavaScript
Dim html As String
html = "<html><head><script type='text/javascript'>" & _
"function add(a, b) { return a + b; }" & _
"</script></head><body></body></html>"
' Load the HTML page
ie.Navigate "about:blank"
Do While ie.Busy
DoEvents
Loop
ie.document.Open
ie.document.Write html
ie.document.Close
' Run the JavaScript function
Dim result As Variant
result = ie.document.parentWindow.execScript("add(5, 3)", "JavaScript")
' Display the result
MsgBox "The result of adding 5 and 3 is: " & result
' Clean up
ie.Quit
Set ie = Nothing
Exit Sub
ErrorHandler:
MsgBox "Error: " & Err.Description
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment