I'm a beginner in VBA and I'm facing problem in selecting country name automatically in web Combo box using cell value from my Excel sheet via loop. It'll be great help if someone could just help me to fix my VBA and XMLHTTP code. My sheet and VBA code is as follows,
Sheet, VBA Code, XML Code below,
1 PP # Nationality DOB Work Permit Number
2 REDACTED Indian 03/01/1978 ?
3 ?
4 ?
5 ?
Sub MOLScraping()
Dim sht As Worksheet
Dim LastRow As Long
Set sht = ThisWorkbook.sheets("MOL")
LastRow = sht.Cells.SpecialCells(xlCellTypeLastCell).Row
Dim IE As New InternetExplorer, HTML As HTMLDocument, post As Object, URL$
URL = "https://eservices.mol.gov.ae/SmartTasheel/Complain/IndexLogin?lang=en-gb"
For i = 2 To LastRow
With IE
.Visible = True
.navigate URL
While .Busy = True Or .readyState <> 4: DoEvents: Wend
Set HTML = .document
HTML.querySelector("button[ng-click='showEmployeeSearch()']").Click
Application.Wait Now + TimeValue("00:00:03") ''If for some reason the script fails, make sure to increase the delay
HTML.getElementById("txtPassportNumber").Value = sht.Range("C" & i)
HTML.getElementById("Nationality").Focus
For Each post In HTML.getElementsByClassName("ng-scope")
With post.getElementsByClassName("ng-binding")
For i = 0 To .Length - 1
If .Item(i).innerText = sht.Range("D" & i) Then ''you can change the country name here to select from dropdown
.Item(i).Click
Exit For
End If
Next i
End With
Next post
HTML.getElementById("txtBirthDate").Value = sht.Range("E" & i)
HTML.querySelector("button[onclick='SearchEmployee()']").Click
HTML.getElementById("TransactionInfo_WorkPermitNumber").innerText = sht.Range("G" & i)
End With
Next x
End Sub
Sub Get_Data()
Dim res As Variant, QueryString$, ID$, Name$
QueryString = "{""PersonPassportNumber"":""REDACTED"",""PersonNationality"":""100"",""PersonBirthDate"":""01/01/1990""}"
With New XMLHTTP
.Open "POST", "https://eservices.mol.gov.ae/SmartTasheel/Dashboard/GetEmployees", False
.setRequestHeader "User-Agent", "Mozilla/5.0"
.setRequestHeader "Content-Type", "application/json"
.send QueryString
res = .responseText
End With
ID = Split(Split(Split(res, "Employees"":")(1), "ID"":""")(1), """,")(0)
Name = Split(Split(Split(res, "Employees"":")(1), "OtherData2"":""")(1), """}")(0)
[A1] = ID: [B1] = Name
End Sub