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

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

February 5th, 2011 Leave a comment Go to comments

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")
  1. June 28th, 2011 at 04:17 | #1

    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

  2. sayed abas
    February 14th, 2012 at 04:13 | #2

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

  1. No trackbacks yet.