IE中未记录/接受VBA Excel值

时间:2018-07-28 00:02:07

标签: excel vba internet-explorer

我对IE无法接收要尝试输入的值(计算机名称)有疑问。我设置了值,它在字段中变得可见,但是当我单击表单上的“提交”时,它(IE)无法识别我输入的值。我可以修改页面上的其他对象,但不能修改此简单的文本字段。设置后,我什至可以从IE中的对象检索值。该字段是必填字段,因此提交例程失败,然后我才能继续。

以下是一些代码:

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

单击“提交”后,网页会弹出错误消息,提示未输入必填字段(计算机名称)。该字段突出显示为红色。

当我手动输入计算机名称时,它可以正常工作,但在使用VBA时却不能。任何帮助将不胜感激!

这里是HTML代码的示例。我将突出显示我要修改的元素。

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

可能重要的注意事项:在稍后的代码行之一中,显示“ style =“ display:none;”的代码段直到在IE中手动将值输入到文件中时才会出现。

谢谢!

1 个答案:

答案 0 :(得分:1)

谢谢蒂姆·威廉姆斯(Tim Williams)的帮助,这些想法使我踏上了寻求解决方案的旅程。原来,我需要触发事件以使页面接受该值。当我意识到这一点时,它非常简单。这是我最终的代码:

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.