最近在處理一個JS例子,由於想不出方法,上網找了一下沒有比較正式解法,卻找到一個骯髒解法,不過也是完成了,方法就是透過新增新節點來達成,記錄一下程式內容以免忘記。
Sub 新增節點()
Dim ie As Object, doc As Object, ele As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.navigate "about:blank"
ie.Visible = False
While ie.Busy
DoEvents
Wend
Set doc = ie.Document
Set ele = doc.createElement("input")
ele.ID = "results"
doc.body.appendChild (ele)
qt = """"
doc.parentWindow.execScript ("document.all[" & qt & "results" & qt & "].value = JSON.stringify(" & qt & "hello 123" & qt & ")")
result = doc.getElementById("results").Value
Debug.Print result
ie.Quit
Set ele = Nothing
Set doc = Nothing
Set ie = Nothing
End Sub
參考資料: