Knowledge.ToString()

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

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

Comments

13 responses to “MS Access VBA: Get/set Javascript Variable in Microsoft Web Browser ActiveX Control”

  1. Stef Avatar
    Stef

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

    1. Vishal Monpara Avatar
      Vishal Monpara

      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 Avatar
    Jeet

    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.

  3. Raju Avatar
    Raju

    Hi,

    I am sucked in a point in macro, I am trying to record macro for Internet explorer. I have open a internet explorer and there is a radio button I have selected the same and submit the page and got some options in which I want to select one and open the same but there is problem it is in java script, so I am not able to open it how get it open if any one please help me to open it.

    Plesssss helppppppppp…….

  4. Chris Read Avatar
    Chris Read

    Cracking little work around. I’ll be using this technique to obtain polygon selections made on a Google Map API FusionsTableLayer, embedded in a webpage.

    Cheers!

  5. Vishal Avatar
    Vishal

    @Lamar Washington
    Make sure that you have injected javascript function checkAll in the page. Otherwise, it will say that checkAll is undefined.

  6. Lamar Washington Avatar
    Lamar Washington

    The function looks like this:

    function checkAll(controlObj)
    {
    var check =new Array();
    check = document.getElementsByName(‘incident’);

    for(var i=0;i<check.length;i++)
    {
    check[i].checked = controlObj.checked;
    }
    }

    The error message indicates checkAll is undefined

  7. Lamar Washington Avatar
    Lamar Washington

    UPDATE:
    The calling onclick looks like this:

    The function looks like this:The function looks like this (this was provided by Phil):

    function checkAll(controlObj)
    {
    var check =new Array();
    check = document.getElementsByName(‘incident’);

    for(var i=0;i<check.length;i++)
    {
    check[i].checked = controlObj.checked;
    }
    }

    When the checkbox with the onclick event is clicked the error message indicates an unknown variable which is the function name checkAll.
    It seems as if the webbrowser control is not recognizing the jscript function

  8. Lamar Washington Avatar
    Lamar Washington

    Thanks, I see how a javascript variable can be set and retrieved. Thank you very much.
    In this case I’ve injected a jscript function into the rendered HTML, added a series of checkboxes with the same name, added an onclick event to the main checkbox pointing to the function. The function simply sets all the checkbox’s to the same state as the clicked checkbox:
    I am using the msaccess webbrowser control to display and run captured html, added html components (checkboxes) and javascript. The rendered html works perfectly. However, in the control the javascript is not working at all. I don’t think the webbrowser control knows what to do with the js. I have a particular html checkbox with an onclick event calling the js function ‘allCheck(this)’. When I click on the checkbox the js is supposed to set all the other check boxes to the same state as the one calling. When I load the html and js in the IE10 browser (using a local file) everything works normally including the js. When I try to execute the same text in the webbrowser control (not loaded from a local file but generated within the program) the HTML is rendered correctly but when clicking the checkbox with the ‘onclick’ event the error message:

    “The value of the property is null or undefined, not a Function object” results.

    It seems the webbrowser control is unaware of the js between the and html tags.

    The calling ‘onclick’ looks like this:

    CODE –>

    The function looks like this (this was provided by Phil):

    CODE –>

    function checkAll(controlObj)
    {
    var check =new Array();
    check = document.getElementsByName(‘incident’);

    for(var i=0;i<check.length;i++)
    {
    check[i].checked = controlObj.checked;
    }
    }

    I appreciate any input as to why the webbrowser control is not processing the js and what I can do to enable it. Or if the control just cannot do this is there a web browser control that does, preferably free?

    Thanks,

  9. flyingscot Avatar
    flyingscot

    This post rocks! Great sample code – works in Access 2007.

  10. James Avatar
    James

    I see the same error: access Denied. This worked for me before in Access 2007 but I recently upgraded to 2010 and the error is popping up.

  11. sayed abas Avatar
    sayed abas

    I see this error
    Run time error. Access Denied: -2147024891 (80070005)
    Can you help me?

  12. Pere Avatar
    Pere

    Looks nice.
    Do you know an easy way to solve de error situation on calling the java functions?
    Say: Run time error. Access Denied: -2147024891 (80070005)

    Thanks

Leave a Reply

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