In your MS Access application, if you are using Microsoft Web browser, we may have to interact with the JavaScript variable inside the loaded page. There is no obvious method exists in the Web browser control but using the following trick you can get/set the JavaScript variable in the webpage.
Quick Tip
Add JavaScript function to get/set the value of JavaScript variable/ HTML element from/to the hidden field. Execute the function using VBA code with the help of DOM functions available in VBA. Please read further for the details or download the code.
Download Example
Download Example of getting/setting JavaScript variable using VBA.
Prerequisite
You should own and be able to add JavaScript functions and hidden field in the Web Page. If you do not own and not able to add JavaScript function and hidden field in the web page, you can inject those using DOM manipulation functions in VBA.
- hdnData – A hidden field
- fnSetHiddenFieldFromVarOrHTML – JavaScript function to set the hidden field hdnData from the JavaScript variable or HTML element
- fnSetVarOrHTMLFromHiddenField – JavaScript function to set the Javascript variable or HTML element value from the hidden field hdnData
- wbInstance – Name of the Web Browser ActiveX Control
Following code assumes that you have hardcoded/injected above mentioned JavaScript functions and hidden field.
How to get value?
Execute the JavaScript function using VBA
wbInstance.Document.parentWindow.execScript("fnSetHiddenFieldFromVarOrHTML")
It will set the hidden field’s value from your interested JavaScript variable/HTML element’s value
Access hidden field using VBA code and get is value in VBA variable.
Dim txtValue as String
txtValue = wbInstance.Document.getElementById("hdnData").Value
How to set value?
Access hidden field using VBA code and set its value.
Dim txtValue as String
txtValue = "My Text"
wbInstance.Document.getElementById("hdnData").Value = txtValue
Execute JavaScript function which will set the desired JavaScript variable/HTML element’s value from hidden field’s value.
wbInstance.Document.parentWindow.execScript("fnSetVarOrHTMLFromHiddenField")
Leave a Reply