【问题标题】:VBA Excel Value not recorded/accepted in IEIE 中未记录/接受 VBA Excel 值
【发布时间】:2018-07-28 00:02:06
【问题描述】:

我遇到了一个问题,即 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 中的文件中之前不会出现。

谢谢!

【问题讨论】:

  • 可能值得检查该字段是否具有动态添加的事件处理程序(keydown?keypress?),这些事件处理程序是通过在字段中键入而触发的,但在使用 VBA 更改值时不会触发。
  • 如果你不介意的话。能否请您也分享一下网址,以便我们进行测试。
  • @Tim Williams:谢谢。好主意!如何检查动态添加的事件处理程序?我可以去哪里了解它们? objCollection.KeyPress 或 objCollection.KeyDown 给出“运行时错误'438':对象不支持此属性或方法。”
  • @user2851376:感谢您提供帮助测试网站。不幸的是,该站点需要公司特定的证书才能访问。不过我可以发布 HTML 代码。

标签: excel vba internet-explorer


【解决方案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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    相关资源
    最近更新 更多