0

So, here's the problem. I've got my code setup essentially to copy/paste some data I've got from excel into a website's textbox. However, I'm at the point where I simply want to code pressing the submit button. However, the submit button stays grey'ed out unless I manually write something into the text-box or manually paste something in. My code actually enters in the data in the text-box, but the submit button still remains grey'ed out and so I can't click it.

Here's the HTML code before and after it has had something written into the text-box resulting in the submit button being either clickable or non-clickable.

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

<div class="form-group">

Here's the full html code for the button:

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

This part of the html code is for the text box, but it's this code that changes in order to enable the button.

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

Any ideas?

Joel Bastien
  • 121
  • 2
  • 11
  • Please share more details so that others can reproduce the issue you encountered. – omegastripes Jul 04 '19 at 19:55
  • have you tried SetAttribute to change the class name of the element? Or looked at input or onchange events? – QHarr Jul 04 '19 at 20:24
  • Hi @QHarr, I took a look at what you mentioned, but can't really find online how the setattribute works. How would this be coded exactly and work? – Joel Bastien Jul 05 '19 at 19:28

1 Answers1

0

From your code and description, I suppose you are validating the TextBox and then append CSS style to the div or submit button, and use it to enable/disable the submit button.

Since I'm not found the method to directly change the CSS style or disabled attribute using VBA.

As a workaround, I suggest you could add a hidden button in your web site, then, in its click event you could check whether the textbox is null and enable/disable the submit button. After setting the value of the textbox, you could trigger this button and validate the textbox value and enable/disable the submit button.

web page code as below:

<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 code like this:

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

The screenshot like this.

Edit:

You could try to use this method: use SendKeys method to enter values into the Textbox, then press the Tab button to trigger the validation rule.

Code as below:

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
Zhi Lv
  • 18,845
  • 1
  • 19
  • 30
  • Thanks! Will try that out. Sorry for my incompetence, but where exactly would i place the webpage code? I understand entirely your VBA code, but, not sure about the webpage code and where would it be placed? – Joel Bastien Jul 05 '19 at 19:42
  • The web page validate function (the above web page code) should be in your website web page. If this website is not developed by yourself or you cannot modify the website source code, above method not apply to you. If that is the case, we need to find another method to change the attribute just from VBA code. – Zhi Lv Jul 08 '19 at 01:40
  • Oh, I see! Sorry should've mentioned, the website is in fact not developed by myself. Therefore, specifically looking for a method to change the attribute with the VBA code only. – Joel Bastien Jul 09 '19 at 15:31
  • Can you use F12 developer tools to check the web site how to validate the form and how to disable/enable the submit button (please explain more detail about this part, it is better to post enough code to reproduce the problem)? If they are using javascript function, perhaps, you could check [this thread](https://stackoverflow.com/questions/31521205/how-to-find-and-call-javascript-method-from-vba) to find and call Javascript functions. – Zhi Lv Jul 10 '19 at 09:29
  • Hi, sure thing! I've inputed the code in my original post. Not sure if this is the webpage html code required, let me know if it's not though! – Joel Bastien Jul 10 '19 at 16:56
  • @ Joel Bastien, still not clear about the validation rule and how it validates, but I suggest you could check the above Edit and try to use the SendKey method to trigger the validation rule. – Zhi Lv Jul 19 '19 at 15:08