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:

Click here to download Example of getting/setting Javascript variable using VBA.

Pre-requisite:

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

About Vishal Monpara

Vishal Monpara is a full stack Solution Developer/Architect with 13 years of experience primarily using Microsoft stack. He is currently working in Retail industry and moving 1's and 0's from geographically dispersed hard disks to geographically dispersed user leveraging geographically dispersed team members.

11 thoughts on “MS Access VBA: Get/set Javascript Variable in Microsoft Web Browser ActiveX Control

  1. 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…….


  2. 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!



  3. 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


  4. 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


  5. 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,


  6. flyingscot

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


  7. 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.


  8. sayed abas

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


  9. 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 *