Question

Is there an easier / direct way to access localstorage variables from an Internet Explorer object except for executing JS to create the value on the DOM ?

I have my own custom solution utilizing a temporary textfield but I wonder if MS provides a direct function.

Function retrieveLocalStorageValue(sURL As String, sLocalStorageVarName As String) As String

     On Error GoTo ErrHandler1:

        Dim javascriptString As String
        Set oBrowser = New InternetExplorer
        oBrowser.Silent = True
        oBrowser.navigate sURL
        oBrowser.Visible = True

        Do
        ' Wait till the Browser is loaded
        Loop Until oBrowser.readyState = READYSTATE_COMPLETE

    On Error GoTo ErrHandlerJscript:

        javastringUrl = "document.body.innerHTML += '<input id=\""test1234\"" type=\""text\"" value=\""'+ localStorage.getItem('" & sLocalStorageVarName & "') +'\""\>';"
        'Execute javascript to create hidden field - Use double quotes in VBA to escape

        'Wait in case it is needed
        Application.Wait DateAdd("s", 1, Now)

        oBrowser.document.parentWindow.eval javastringUrl
        retrieveLocalStorageValue = oBrowser.document.getElementById("test1234").getAttribute("value")

    Exit Function

ErrHandler1:
    MsgBox ("Error, debugging required")
    retrieveLocalStorageValue = "error"

ErrHandlerJscript:
    MsgBox ("Error with javascript execution, debugging required")
    retrieveLocalStorageValue = "error"
End Function


Sub test()
    Dim test As String
    test = retrieveLocalStorageValue("http://127.0.0.1/stackexchange/localStorageVBA.html", "testObject")
    MsgBox test
End Sub

Reference: http://maythesource.com/2014/04/22/vba-read-localstorage-variable-from-internet-explorer-object-using-temporary-textfield/

Était-ce utile?

La solution

No not really. Yes, you can set a reference to Microsoft Internet Explorer and a reference to Microsoft HTML Object Library. This will allow you to access the DOM object model directly, and the intellisense will work. But if you actually try to access the property, it will throw an error. (As demonstrated below:)

Option Explicit

Sub Example()
    ThisFails "http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_webstorage_local", "lastname"
End Sub

Function ThisFails(ByVal sURL As String, ByVal sLocalStorageVarName As String) As String
    Dim oBrowser As SHDocVw.InternetExplorer
    Dim hDoc As MSHTML.HTMLDocument
    Dim hWin As MSHTML.HTMLWindow2
    Set oBrowser = New SHDocVw.InternetExplorer
    oBrowser.Silent = True
    oBrowser.Navigate sURL
    oBrowser.Visible = True

    Do
        ' Wait till the Browser is loaded
    Loop Until oBrowser.ReadyState = READYSTATE_COMPLETE
    Do
        Set hDoc = oBrowser.Document
        'Trust me on these loops
    Loop While hDoc Is Nothing
    Do
        Set hWin = hDoc.parentWindow
    Loop While hWin Is Nothing

    ThisFails = hWin.localStorage.getItem(sLocalStorageVarName)


End Function
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top