MS Access VBA: Get/set Javascript Variable in Microsoft Web Browser ActiveX Control

Category: MS AccessTags: , , , ,

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")

Share

13 comments

Your email address will not be published. Required fields are marked *

  1. Stef says:

    Thank you for this explanation. But how would i populate a table with data? Can i send a JSON object?

    1. Hi Stef,

      You can definitely send JSON object. Please note that Web Browser control may use IE 7 rendering regardless of which IE is installed on your computer. So certain JSON functions may not be available natively. You may need to use polyfill JavaScript.

      Regards,
      Vishal Monpara

  2. Jeet says:

    To resolve this error: Run time error. Access Denied: -2147024891 (80070005)
    You need to run the Access exe with Admin privileges.
    This is a very common issue with browser automation.