Created
July 19, 2024 07:41
-
-
Save muhdkhokhar/47d42812343bb0d25797770fb471fefb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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