Link to home
Start Free TrialLog in
Avatar of happyahmad
happyahmad

asked on

Calling a JavaScript Function from within VB 6

I am automating IE via Com and Dom in Visual Basic 6.
I am able to access the HTML tag elements inorder to populate the fields, but am coming accross one issue that involves javascript that  is in the webpage.
I navigate to the webpage then there is a drop down list that users can pick a given item. Once they select an item from the list - a javascript function (via the OnChange event fires).  This JavaScript function called "set_assign_to()" then assigns 2 other HTML input fields based on the option choosen from the drop down list.
The webpage source that deals with the list is below:
<select name="component" onchange="set_assign_to();" size="5">
          <option value="App Upload XForm">App Upload XForm          </option>
          <option value="Case Clock">Case Clock          </option>
          <option value="Case Cloning">Case Cloning          </option>
          <option value="Case Escape">Case Escape          </option>
          <option value="Case Stuck">Case Stuck          </option>
          <option value="Cash Rules / Processing">Cash Rules / Processing          </option>
          <option value="Cash Wars">Cash Wars          </option>
          <option value="Closeout Rules / Processing">Closeout Rules / Processing          </option>
          <option value="Companions / Additionals">Companions / Additionals          </option>
          <option value="Compliance - New forms &amp; NPI">Compliance - New forms &amp; NPI          </option>
          <option value="DashBoard">DashBoard          </option>
          <option value="DataBase Updates">DataBase Updates          </option>
          <option value="DE Rules / Processing">DE Rules / Processing          </option>
          <option value="Doc Gen-Amendment">Doc Gen-Amendment          </option>
          <option value="Doc Gen-Emails">Doc Gen-Emails          </option>
          <option value="Doc Gen-Letters">Doc Gen-Letters          </option>
          <option value="Doc Gen-Part1/Part2">Doc Gen-Part1/Part2          </option>
          <option value="Doc Routing Rules / Processing">Doc Routing Rules / Processing          </option>
          <option value="Downloads / Extracts">Downloads / Extracts          </option>
          <option value="EM Rules / Processing">EM Rules / Processing          </option>
          <option value="ErrorQ and Critical Bugs">ErrorQ and Critical Bugs          </option>
          <option value="FileNet Interface (Imaging)">FileNet Interface (Imaging)          </option>
          <option value="General Rules / Processing">General Rules / Processing          </option>
          <option value="GetWork / Tasks">GetWork / Tasks          </option>
          <option value="Indexing Rules / Processing">Indexing Rules / Processing          </option>
          <option value="IRTS">IRTS          </option>
          <option value="Issue Rules / Processing">Issue Rules / Processing          </option>
          <option value="Licensing &amp; CC Rules / Processing">Licensing &amp; CC Rules / Processing          </option>
          <option value="MQ Hub &amp; Mainframe">MQ Hub &amp; Mainframe          </option>
          <option value="Obselete: AWD Interface (Imaging)">Obselete: AWD Interface (Imaging)          </option>
          <option value="Poller">Poller          </option>
          <option value="Replacement Rules / Processing">Replacement Rules / Processing          </option>
          <option value="Reporting / Analysis">Reporting / Analysis          </option>
          <option value="Requirements - Admin">Requirements - Admin          </option>
          <option value="Requirements - Medical">Requirements - Medical          </option>
          <option value="Settlement Rules / Processing">Settlement Rules / Processing          </option>
          <option value="SIPS Interfaces">SIPS Interfaces          </option>
          <option value="System and Database Admin">System and Database Admin          </option>
          <option value="System Stability/Performance">System Stability/Performance          </option>
          <option value="TDC Rules / Processing">TDC Rules / Processing          </option>
          <option value="Troubleshooting">Troubleshooting          </option>
          <option value="Undetermined">Undetermined          </option>
          <option value="User Training/Procedures">User Training/Procedures          </option>
          <option value="Utilities - Common Libraries">Utilities - Common Libraries          </option>
          <option value="Utilities - Log4J Framework">Utilities - Log4J Framework          </option>
          <option value="Utilities - Security &amp; LDAP">Utilities - Security &amp; LDAP          </option>
          <option value="UW Rules / Processing">UW Rules / Processing          </option>
          <option value="Web UI - Cash Wars">Web UI - Cash Wars          </option>
          <option value="Web UI - Commission Coder">Web UI - Commission Coder          </option>
          <option value="Web UI - Commission Rep">Web UI - Commission Rep          </option>
          <option value="Web UI - Data Entry">Web UI - Data Entry          </option>
          <option value="Web UI - Exception Manager">Web UI - Exception Manager          </option>
          <option value="Web UI - Indexer">Web UI - Indexer          </option>
          <option value="Web UI - Over All">Web UI - Over All          </option>
          <option value="Web UI - Underwriter">Web UI - Underwriter          </option>
          <option value="Workflow">Workflow          </option>
      </select>
    </td>
  </tr>
The JavaScript function in the Web page looks like:
function set_assign_to() {
    // Based on the selected component, fill the "Assign To:" field
    // with the default component owner, and the the "QA Contact:" field
    // with the default QA Contact.
    var form = document.Create;
    var assigned_to = form.assigned_to.value;

    var qa_contact = form.qa_contact.value;

    var index = -1;
    if (form.component.type == 'select-one') {
        index = form.component.selectedIndex;
    } else if (form.component.type == 'hidden') {
        // Assume there is only one component in the list
        index = 0;
    }
    if (index != -1) {
        var owner = initialowners[index];
        var component = components[index];
        if (assigned_to == last_initialowner
            || assigned_to == owner
            || assigned_to == '') {
            form.assigned_to.value = owner;
            last_initialowner = owner;
        }
            var contact = initialqacontacts[index];
            if (qa_contact == last_initialqacontact
                || qa_contact == contact
                || qa_contact == '') {
                  form.qa_contact.value = contact;
                  last_initialqacontact = contact;
            }
    }
}
My Visual Basic 6 code :
     
    Dim gIE As Object
    Set gIE = CreateObject("InternetExplorer.Application")
    gIE.Visible = True
    gIE.Navigate2 strGeniusBugzillaURL
    Do
            DoEvents
    Loop While gIE.Busy = True
   
   'Here I choose one of the options in the drop down list
   gIE.document.getElementsByName("component").Item(0).Options(14).Selected = True
   
    'Attempting to call the JavaScript function on the webpage that assigns the "Assign To:"
    'and the "QA Contact:" fields based on the component selection.
    gIE.document.parentWindow.execScript ("set_assign_to")


When I run the above code it correctly loads IE with the Correct Option selected in the "Component" drop down list.
Then I get an error message that pops up from the last line of code trying to execute the "set_assign_to" javascript function, "Run-time error '-2147024891 (80070005)' Access is denied."

What am I doing wrong here?










Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Change it to this:

   'Here I choose one of the options in the drop down list
   gIE.document.getElementsByName("component").Item(0).Options(14).Selected = True
   
    'Attempting to call the JavaScript function on the webpage that assigns the "Assign To:"
    'and the "QA Contact:" fields based on the component selection.
    '''' gIE.document.parentWindow.execScript ("set_assign_to")
   gIE.document.getElementsByName("component").Item(0).onchange()

If that works you can also try to access elements by name like this:

Change it to this:

   'Here I choose one of the options in the drop down list
   gIE.document.Create.component.Options(14).Selected = True
   gIE.document.Create.component.onchange()


And full syntax is this:

   'Here I choose one of the options in the drop down list
   gIE.document.Forms.Create.Elements.component.Options(14).Selected = True
   gIE.document.Forms.Create.Elements.component.onchange()



Avatar of happyahmad
happyahmad

ASKER

Hello Zvonko,

In trying your suggestion my code looks like the below:

Dim test As Variant
Dim gIE As Object
   
    Set gIE = CreateObject("InternetExplorer.Application")
    gIE.Visible = True
    gIE.Navigate2 strGeniusBugzillaURL
   
    Do
            DoEvents
    Loop While gIE.Busy = True
   
   'Your Suggestion code:  
   gIE.document.Forms.Create.elements.component.Options(14).Selected = True
   test = gIE.document.Forms.Create.elements.component.onchange()
   
Although this ran fine (without any runtime errors), It correctly selected the right drop down option in the list (it is highlighed), but for some reason the 2 fields on the web page that are supposed to get populated remain blank? While you use the web page manually and click each selection in the drop down list it immediately updates these 2 input fields, but when I run the above code it leaves those same 2 fields blank? Any ideas....not sure why???

I have today no access to a VB6 test environment, but could you check wether removing the braces does call the script.
Call the event handler like this statement line:

gIE.document.Forms.Create.elements.component.onchange


ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello Zvonko,
You helped me greatly with this problem and I appreicate it. Although, I could not get the function to call I got to a point because of your help where I could avoid calling the javascript function (not the most optimal solution but a solution that does work!) Thanks....I still have much learning to do :)