使用VBA从excel复制/粘贴到IE网页时,如何显示提交按钮?

时间:2019-07-04 19:23:34

标签: excel vba internet-explorer

所以,这就是问题所在。我已经完成了代码设置,本质上是将从excel中获得的一些数据复制/粘贴到网站的文本框中。但是,我只是想按提交按钮进行编码。但是,除非我手动将一些内容写到文本框中或手动粘贴一些内容,否则提交按钮保持为灰色。我的代码实际上输入了文本框中的数据,但是提交按钮仍然保持为灰色,并且所以我不能点击它。

这是在文本框中写入内容之前和之后的H​​TML代码,导致提交按钮可单击或不可单击。

<div class="form-group has-error">

<div class="form-group">

这是按钮的完整html代码:

<button disabled="" class="pull-right report-module-query-list-execute-button btn btn-sm btn-default" type="button"><span>Print</span></button>

这部分html代码用于文本框,但是更改此代码是为了启用按钮。

<div class="form-group has-error">
<label class="col-sm-4 col-sm-offset-1 control-label" for="TaskStateHistoryPivot_1"><span>Search data in</span></label>
<div class="col-sm-7">
<div class="col-sm-12">
<textarea class="report-module-query-execution-freeform-area" rows="5"></textarea></div></div></div>

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

根据您的代码和描述,我想您正在验证TextBox,然后将CSS样式附加到div或Submit按钮,并使用它来启用/禁用Submit按钮。

由于我找不到使用VBA直接更改CSS样式或禁用属性的方法。

作为一种解决方法,我建议您可以在网站中添加一个隐藏按钮,然后在其单击事件中检查文本框是否为空并启用/禁用提交按钮。设置文本框的值后,您可以触发此按钮并验证文本框的值并启用/禁用提交按钮。

网页代码如下:

<script src="https://code.jquery.com/jquery-3.4.1.js"></script>
<script>
    $(function () {
        $("#btnSubmit").prop("disabled", true);
        function validate() {
            if ($("#Text1").val() != '') {
                $("#btnSubmit").prop("disabled", false);
            }
            else {
                $("#btnSubmit").prop("disabled", true);
            }
        }
        $("#Text1").change(function () {
            validate();
        });
        $("#btnvalidte").click(function () {
            validate();
        });
    });
</script>
<div>
    <input id="Text1" type="text" value="" />
    <input id="btnvalidte" type="button" value="validate" style="display:none" />
    <input id="btnSubmit" type="button" value="Submit" />
</div>

像这样的VBA代码:

Sub Test()
    Dim ie As Object
    Dim Rank As Object
    Set ie = CreateObject("InternetExplorer.application")
    ie.Visible = True
    ie.Navigate ("http://localhost:54382/HtmlPage47.html")
    Do
        If ie.ReadyState = 4 Then
            Exit Do
        Else
        End If
    Loop

    Set doc = ie.document        

    'get the excel data

    Dim val As String
    val = Range("C2").Value

    'find the textbox and set the value

    doc.getElementById("Text1").Value = val

    'trigger the validate button to check whether the textbox is empty and enable the submit button

    doc.getElementById("btnvalidte").Click
End Sub

屏幕截图类似this

修改

您可以尝试使用此方法:使用SendKeys方法在文本框中输入值,然后按Tab键触发验证规则。

代码如下:

Sub Test()
    Dim ie As Object
    Dim Rank As Object
    Set ie = CreateObject("InternetExplorer.application")
    ie.Visible = True
    ie.Navigate ("http://localhost:54382/HtmlPage47.html")
    Do
        If ie.readyState = 4 Then
            Exit Do
        Else
        End If
    Loop

    Set doc = ie.document

    'get the excel data

    Dim val As String
    val = Range("C2").Value

    'find the textbox and set the value

    Set TextBoxfield = doc.getElementById("Text1")
    'set focus on the textbox.
    With TextBoxfield
        .Focus
    End With
    'enter value into the textbox.
    SendKeys CStr(val)
    'press the Tab button to change the focus and try to trigger the validation rule
    SendKeys "{TAB}"    

    While ie.Busy Or ie.readyState < 4: DoEvents: Wend
End Sub