Search code examples
excelvbainternet-explorer-11

VBA using .click on getelment by id keeps loading


I'm creating a script using VBA in excel to open a webpage in internet explorer and push data from Excel to submit the page and move to the next one.

In addition to pushing data to replace the unit image file by clicking the remove button: The remove button

and its code as following :

<button type="submit" id="edit-field-building-autocad-img-und-0-remove-button" name="field_building_autocad_img_und_0_remove_button" value="remove" class="btn btn-danger form-submit icon-before ajax-processed ladda-button button btn-sm delete-file-btn" data-style="zoom-in" data-spinner-color="#000" data-spinner-lines="12"><span class="ladda-label"><span class="icon glyphicon glyphicon-trash" aria-hidden="true"></span>  remove</span><span class="ladda-spinner"></span></button>

I use the button id with the .click

It should look like that after I click the button: afer clicking the remove button

But it just keeps loading and looks that way: Keep loading

My code:

Sub PushDataMoh()

Dim IE  As Object
Dim doc As HTMLDocument
Dim wsb As Worksheet: Set wsb = ThisWorkbook.Sheets("sheet2")
Dim pn     'page number
Dim mypage 'The default page
Dim RowCount
Dim UnitNum
Dim LevelNum


For RowCount = 2 To 4
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    pn = wsb.Range("A" & RowCount).Value
    mypage = "https://" & pn & "/edit/320189/320225"
    IE.Navigate mypage

    Do While IE.Busy Or IE.ReadyState <> 4
        Application.Wait DateAdd("s", 1, Now)
    Loop

    Set doc = IE.Document
    Set submit = doc.getElementById("edit-submit")
    Set del = doc.getElementById("edit-field-building-autocad-img-und-0-remove-button")

    UnitNum = doc.getElementById("edit-field-unit-no-und-0-value").Value   'Get unit number
    LevelNum = doc.getElementById("edit-field-floor-no-und-0-value").Value  'Get Level number
    NameCheck = "The Unit number is " & UnitNum & " the level number is " & LevelNum
    Debug.Print NameCheck

    doc.getElementById("edit-field-north-side-und-0-value").Value = wsb.Range("E" & RowCount).Value     'Get North
    doc.getElementById("edit-field-east-side-und-0-value").Value = wsb.Range("F" & RowCount).Value      'Get East
    doc.getElementById("edit-field-south-side-und-0-value").Value = wsb.Range("G" & RowCount).Value     'Get South
    doc.getElementById("edit-field-west-side-und-0-value").Value = wsb.Range("H" & RowCount).Value      'Get West

    del.Click

    Do While IE.Busy Or IE.ReadyState <> 4
        Application.Wait DateAdd("s", 10, Now)
    Loop

    Set choseFile = doc.getElementById("edit-field-building-autocad-img-und-0-upload")
    Set upload = doc.getElementById("edit-field-building-autocad-img-und-0-upload-button")

    choseFile.Click
    upload.Click

    Do While IE.Busy Or IE.ReadyState <> 4
        Application.Wait DateAdd("s", 1, Now)
    Loop

    submit.Click

    Do While IE.Busy Or IE.ReadyState <> 4
        Application.Wait DateAdd("s", 1, Now)
    Loop
    'RowCount.EntireRow.Interior.ColorIndex = 6

    'IE.Quit

Next

End Sub

The HTML code as following:

<div class="file-widget form-managed-file clearfix col-md-9 files-cont"><input type="hidden" name="field_building_autocad_img[und][0][fid]" value="820983">
<input type="hidden" name="field_building_autocad_img[und][0][display]" value="1">
<span class="file"><img class="file-icon" alt="Image icon" title="image/png" src="/modules/file/icons/image-x-generic.png"> <a href="https://subdivision-services.housing.gov.sa/sites/default/files/1577728621274522778-test_part1-11_3.png" type="image/png; length=508614">1577728621274522778-test_part1-11_3.png</a></span>  <span class="file-size badge">496.69 كيلوبايت</span><button type="submit" id="edit-field-building-autocad-img-und-0-remove-button" name="field_building_autocad_img_und_0_remove_button" value="حذف" class="btn btn-danger form-submit icon-before ajax-processed ladda-button button btn-sm delete-file-btn" data-style="zoom-in" data-spinner-color="#000" data-spinner-lines="12"><span class="ladda-label"><span class="icon glyphicon glyphicon-trash" aria-hidden="true"></span>
 حذف</span><span class="ladda-spinner"></span></button>
</div>

Solution

  • You can try to refer to this example may help to dispatch the event correctly.

    Option Explicit
    
    'It works with Excel 32 bit and Excel 64 bit
    #If Win64 Then
      'For 64 bit systems
      Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
    #Else
      'For 32 bit systems
     ' Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    #End If
    
    Sub test()
    
    Dim url As String
    Dim browser As Object
    Dim btn As Object
    
      url = "https://example.com"
    
      Set browser = CreateObject("internetexplorer.application")
      browser.Visible = True
      browser.navigate url
      Do Until browser.readyState = 4: DoEvents: Loop
    
      Set btn = browser.document.getElementById("btn1")
      btn.Focus
    
      'btn.Click
    
      Call TriggerEvent(browser.document, btn, "click")
    
    End Sub
    
    Private Sub TriggerEvent(htmlDocument As Object, htmlElementWithEvent As Object, eventType As String)
    
      Dim theEvent As Object
    
      htmlElementWithEvent.Focus
      Set theEvent = htmlDocument.createEvent("HTMLEvents")
      theEvent.initEvent eventType, True, False
      htmlElementWithEvent.dispatchEvent theEvent
    End Sub
    

    You can try to modify the sample and try to make a test on your end and let us know about your test results. Whether it works or not.