Home » excel » VBA Excel Value not recorded/accepted in IE

VBA Excel Value not recorded/accepted in IE

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have a problem with IE not accepting a value (the name of a computer) that I am trying to put into a field. I set the value, it becomes visible in the field, but then when I click “Submit” on the form, it (IE) fails to recognize the value I entered. I am able to modify other objects on the page, but not this simple text field. I can even retrieve the Value from the object in IE after setting it. The field is required, so the submit routine fails before I can proceed.

Here’s some code:

'Find the correct instance of IE
Set objShell = CreateObject("Shell.Application")
' etc.
Set IE = objShell.Windows(x)
' etc.

' Enter Computer_Name
Set objCollection = IE.Document.getElementsByTagName("iframe")(2).contentDocument.getElementById("QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC")
objCollection.Value = Computer_Name ' (The text I'm trying to enter)

' Some other stuff that is working

' Click the "Submit" button on the form (this works too).

After clicking “Submit”, the webpage pops up an error saying that the required field (the computer name) was not entered. The field is highlighted red.

When I enter the computer name manually, it works just fine, but not when using VBA. Any help would be appreciated!

Here is a sample of the HTML code. I’ll highlight the element I’m trying to modify.

<div class="questionContainer ">
    <div class="left questionBody" id="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC-label-body" required="false">
        <label class="questionlabel bold" id="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC-label" for="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC">Computer Name<span class="required">*</span></label>
    </div>
    <div class="left answerBody block">
        <div class="left">
            <div id="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC-answer-body">
' *********** The next line is the element in question. ************
                <input tabindex="0" class="answerTextarea " id="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC" aria-describedby="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC-instructions" aria-labelledby="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC-label" required="true" type="text" size="40" value="" responsetype="TEXT" questiondefid="QDHAA5V0GH4LSAO2AI6F2MXNIAJ5CE" totalorder="3" questionid="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC" level="0" overwrite="1" maxlng="16" wrap="virtual">
            </div>
        </div>
        <div class="validationResult clear" id="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC-validationResult" style="display: none;"></div>
        <div class="clear"></div>
        <div class="instructions" id="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC-instructions"></div>
                <div class="clear"></div>
    </div>
    <div class="clear"></div>
</div>

Possibly important note: the bit of code in one of the later lines of code that says “style=”display: none;” does not appear until the value is entered into the filed manually in IE.

Thanks!

How to&Answers:

Thank you Tim Williams for your help with ideas which put me on the journey that led to this solution. It turned out that I needed to trigger the events for the page to accept the value. Amazingly simple, when I come to realize it. Here is what my final code looks like:

Dim objEvent ' **** NEW ****

'Find the correct instance of IE (opened manually initially)
Set objShell = CreateObject("Shell.Application")
' etc.
Set IE = objShell.Windows(x)
' etc. (including navigation when necessary)

' After the webpage has loaded, set the event handler ' **** NEW ****
Set objEvent = IE.Document.createEvent("HTMLEvents") ' **** NEW ****

' Enter Computer_Name
Set objCollection = IE.Document.getElementsByTagName("iframe")(2).contentDocument.getElementById("QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC")
objCollection.Value = Computer_Name ' (The text I'm entering)
' Trigger the event change ' **** NEW ****
objEvent.initEvent "change", False, True ' **** NEW ****
objCollection.dispatchEvent objEvent ' **** NEW ****

' Enter other needed information

' Click the "Submit" button on the form.