This specific content was written 12 years ago.
Please keep this in mind as it may be outdated and not adhering to best-practices.
Here is a toy solution to access localstorage variables if they are not accessible directly.
Imagine we have test HTML webpage with HTML:
<html>
<body>
<script>
// Put the object into storage
localStorage.setItem('testObject', 'testString');
// Retrieve the object from storage
var retrievedObject = localStorage.getItem('testObject');
</script>
</body>
</html>
We can use the following VBA code to access the localstorage variable.
The code below executes javascript code that creates a temporary text field with the localstorage value. Following we are able to access the string value with getElementById and getAttributeValue.
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
Mr. Menelaos Bakopoulos is currently pursuing his PhD both at Center for TeleInFrastruktur (CTiF) at Aalborg University (AAU) in Denmark and Athens Information Technology (AIT) in Athens, Greece. He received a Master in Information Technology and Telecommunications Systems from Athens Information Technology and a B.Sc. in Computer Science & Management Information Systems from the American College of Thessaloniki. Since April 2008 he has been a member of the Multimedia, Knowledge, and Web Technologies Group.
More Posts
Apr 22 2014
VBA: Read localstorage variable from Internet Explorer Object using temporary textfield
Here is a toy solution to access localstorage variables if they are not accessible directly.
Imagine we have test HTML webpage with HTML:
<html> <body> <script> // Put the object into storage localStorage.setItem('testObject', 'testString'); // Retrieve the object from storage var retrievedObject = localStorage.getItem('testObject'); </script> </body> </html>We can use the following VBA code to access the localstorage variable.
The code below executes javascript code that creates a temporary text field with the localstorage value. Following we are able to access the string value with getElementById and getAttributeValue.
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 SubMenelaos Bakopoulos
Mr. Menelaos Bakopoulos is currently pursuing his PhD both at Center for TeleInFrastruktur (CTiF) at Aalborg University (AAU) in Denmark and Athens Information Technology (AIT) in Athens, Greece. He received a Master in Information Technology and Telecommunications Systems from Athens Information Technology and a B.Sc. in Computer Science & Management Information Systems from the American College of Thessaloniki. Since April 2008 he has been a member of the Multimedia, Knowledge, and Web Technologies Group.
More Posts
Related posts:
By Menelaos Bakopoulos • Programming, Scripts 0 • Tags: IE, LocalStorage, VBA