3

I am trying to execute some JS code inside my excel workbook, so I found out about the Internet Explorer & Microsoft HTML references, and I built this code with some inspiration from https://stackoverflow.com/a/31532085 -

Dim hf As Integer: hf = FreeFile
Dim data As String

Open "C:\Users\karti\Desktop\3CD-MRL v4 testing\test.json" For Input As #hf
    data = Input$(LOF(hf), #hf)
Close #hf

Debug.Print data
' json data received from file, now sending it to the js code

Dim objIE As SHDocVw.InternetExplorer
Dim currentWindow As HTMLWindowProxy
Dim Url As String: Url = "C:\Users\karti\Desktop\3CD-MRL v4 testing\test.html"
Dim codez As String: codez = "func = function(){return myFuncReturns('" & data & "');}; retVal = func();"
Set objIE = New SHDocVw.InternetExplorer
objIE.navigate Url
objIE.Visible = 0

Do While objIE.readyState <> 4
    DoEvents
Loop

Set currentWindow = objIE.document.parentWindow

currentWindow.execScript code:=codez
Dim result As Long
result = objIE.document.Script.retVal 'here i am supposed to get the updated json
Debug.Print result
objIE.Quit
Set objIE = Nothing

and here's my test.html file's code (which is set to the URL variable in VBA code) -

<!-- saved from url=(0016)http://localhost -->
<html>
<head>
    <script type="text/javascript">
        function myFuncReturns(json) {
            let jsonObject = JSON.parse(json);
            jsonObject["status"] = "done";
            return JSON.stringify(jsonObject);
        }
    </script>
</head>
</html>

My JS code is very simple, receive json in string, convert to object, add a parameter named status to the json, re-convert updated object to string, and return the string json to vba. In my VBA code, I just read data from a local json file, send it to my html file using IE reference, and receive updated json string into the result variable - but I am getting 'Runtime error 438 - Object doesn't support this property or method' error and this gets highlighted, with result=0 showing in debugger -

What's wrong with my code? What should I do? Kindly guide... Thanks! :)


Edit: Just for debugging, I removed return JSON...; from JS and made it into an alert with alert(...); and also temporarily removed the result = ... line from VBA (as it has no use with an alert). And it works! I received a popup on my screen containing the updated JSON after running the macro. It means that my VBA code is sending the data, it's just not receiving it into the result variable. What could be wrong?

kartik
  • 550
  • 1
  • 6
  • 19
  • Your code looks like it is just trying to update JSON, correct? I would recommend using the [VBA-JSON](https://github.com/VBA-tools/VBA-JSON) code that is on GitHub. This code will Parse JSON and convert back to JSON as well. – Robert Todar Aug 15 '22 at 19:34
  • @RobertTodar Thanks for replying! Just for the sake of asking this question I shortened my code - sorry about that. Actually my code takes the json, converts it into xml and is supposed to send the xml string back - I have all JS side figured out, just can't get the string back into the result variable. For now I just want to simply check if I receive the updated JSON - if it works, I'll complete my script for the xml conversion... _(also I have added some more details to question - please check)_ Thanks again! :) – kartik Aug 16 '22 at 05:17
  • As far as I know, JSON.stringify(obj) returns a json string, but I found that you define a variable of type Long to receive the returned result, could this be the cause of the problem? – Xudong Peng Aug 16 '22 at 08:16
  • Hi, @XudongPeng! Thanks for replying - I tried defining as `String`, `Object`, & `Variant`, but nothing works, still the same 'Object doesn't support this property or method' error, with `result=""` coming in debugging when defined as String, `result=Empty` when def. as Variant, and `result=Nothing` coming when def. as Object. What should I do? Kindly guide... Thanks again! – kartik Aug 16 '22 at 11:06

0 Answers0