Search code examples
vbaexcelie-automation

VBA code works in debug mode, but fails in run mode


I am trying to fill one input field in Internet explorer, but the code is only working in debug mode, while I am pressing F8 on each of the actions. When I am running the code automatically, the value fill is not working, the input field remains empty.

What could be the issue?

Here is the HTML part of the code:

<input tabindex="0" class="select__input" type="text">
</div>
      <div class="sm__amount" name="payment">
          <div class="sm__send">
              <div class="amount selected" id="amount_get">
                   <div class="amount__field">
                      <div class="amount__label selected">Send amount</div>
      <input id="input-amount_get" type="text" value="" autoComplete="off">
                      </div>
                      <div class="amount__select" id="amount-select">
                          <div class="amount__select__value">
                             XXX
                              <div class="amount__select__trl"></div>
                           </div>
                           <div class="amount__select__box">
                               <div id="currency-XXX" data-v="XXX">XXX</div>
                               <div id="currency-XXX" data-v="XXX"XXX</div>
                     </div>
               </div>
         </div>
  </div>

I am trying to fill the field with ID input-amount_get

Below are the options that i have tried, I commented out the other options, as they did not help. As i said, when I am going through this part of the code with F8, it works just fine.

 Application.Wait (Now + TimeValue("0:00:03"))
    'objIE.document.getElementById("input-amount_get").Focus
    'objIE.document.getElementsByTagName("input")(1).innerText = "500"
    objIE.document.getElementById("input-amount_get").innerText = "500.00"
    'objIE.document.getElementById("input-amount_get").Click
    'objIE.document.getElementById("input-amount_get").FireEvent ("onchange")
    Application.Wait (Now + TimeValue("0:00:05"))

Here is the full code that I am using:

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub TestBot()

    Dim objIE As InternetExplorer
    Dim doc As HTMLDocument
    Dim el As Object


    Set objIE = New InternetExplorer

    objIE.Visible = True

    objIE.navigate "Website"

    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

    Do
        Set el = Nothing
        On Error Resume Next
        Set el = objIE.document.getElementById("login")
        On Error GoTo 0
        DoEvents
        Sleep 500
    Loop While el Is Nothing


    objIE.document.getElementById("login").Click

    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

 Do
        Set el = Nothing
        On Error Resume Next
        Set el = objIE.document.getElementsByClassName("button submit")(0)
        On Error GoTo 0
        DoEvents
        Sleep 500
    Loop While el Is Nothing

     Set doc = objIE.document


    objIE.document.getElementsByTagName("input")(0).innerText = "[email protected]"

    objIE.document.getElementsByTagName("input")(3).innerText = "Password"

    objIE.document.getElementsByClassName("button submit")(0).Click


    Do
        Set el = Nothing
        On Error Resume Next
        Set el = objIE.document.getElementById("account_menu")
        On Error GoTo 0
        DoEvents
        Sleep 500
    Loop While el Is Nothing



    objIE.document.getElementsByClassName("profile__button")(0).Click

   Do
        Set el = Nothing
        On Error Resume Next
        Set el = objIE.document.getElementById("wrapBox")
        On Error GoTo 0
        DoEvents
        Sleep 500
    Loop While el Is Nothing

    Application.Wait (Now + TimeValue("0:00:01"))
    objIE.document.getElementsByClassName("select__trl")(0).Click
    Application.Wait (Now + TimeValue("0:00:01"))
    objIE.document.getElementsByName("US")(0).Click

      Do
        Set el = Nothing
        On Error Resume Next
        Set el = objIE.document.getElementsByClassName("preloader__loader")(0)
        On Error GoTo 0
        DoEvents
        Sleep 1000
    Loop While Not (el Is Nothing)

    Do Until objIE.readyState <> 4: DoEvents: Loop

    Application.Wait (Now + TimeValue("0:00:03"))
    DoEvents
    objIE.document.getElementById("input-amount_get").innerText = "500.00"
    Application.Wait (Now + TimeValue("0:00:05"))

    Do
        Set el = Nothing
        On Error Resume Next
        Set el = objIE.document.getElementsByClassName("preloader__loader")(0)
        On Error GoTo 0
        DoEvents
        Sleep 500
    Loop While Not (el Is Nothing)

    Application.Wait (Now + TimeValue("0:00:03"))
    objIE.document.getElementsByName("button_submit")(0).Click


    objIE.Quit

End Sub

Solution

  • So finally after experimenting with various solutions I found out that somehow the code was running faster than it should. I managed to solve the issue by adding some additional wait sentences. Also I am using separate Private Sub for setting the delay:

    Private Sub delay(seconds As Long)
        Dim endTime As Date
        endTime = DateAdd("s", seconds, Now())
        Do While Now() < endTime
            DoEvents
        Loop
    End Sub
    

    And the code lines for entering the text now look like:

    DoEvents 'not necessary, but I left it just in case
    delay 4
    objIE.document.getElementById("input-amount_get").innerText = "500"
    

    When these lines are reached, the code waits for 4 additional seconds and then sets the value to 500.