I'm having trouble to Submit a form via VBA.
The problem is that the data I enter is correct but once I click the button I get an error. It says:
"No data found" and it errased the ID number which now says "Required".
When I do this manually everything works fine. Any clues why this is? I mean, what is the difference between clicking a button manually or programmatically.
Website: http://www.rdc.gov.ae/register.aspx
Fill in:
ID Type: Unique Number
ID: 35053392
(P.S., the data is not classified or confidential/sensitive)
Before:
After:
This is the vba code:
This is the HTML
The problem is that the data I enter is correct but once I click the button I get an error. It says:
"No data found" and it errased the ID number which now says "Required".
When I do this manually everything works fine. Any clues why this is? I mean, what is the difference between clicking a button manually or programmatically.
Website: http://www.rdc.gov.ae/register.aspx
Fill in:
ID Type: Unique Number
ID: 35053392
(P.S., the data is not classified or confidential/sensitive)
Before:
After:
This is the vba code:
Code:
Option Explicit
'/ Win API declaration
Private Declare PtrSafe Function ShowWindow Lib "user32" _
(ByVal hwnd As LongPtr, ByVal nCmdSHow As Long) As Long
Const SW_SHOWMAXIMIZED = 3
Sub Login_RDC()
'Declaration section
Dim IE As New SHDocVw.InternetExplorer
Dim URL
Dim htmlDoc As New MSHTML.HTMLDocument
Dim htmlOptions As MSHTML.IHTMLElementCollection
Dim htmlOption As MSHTML.IHTMLElement
Dim htmlInputs As MSHTML.IHTMLElementCollection
Dim htmlInput As MSHTML.IHTMLElement
'Open Internet Explorer and navigate to webpage
IE.Visible = True
IE.navigate "http://www.rdc.gov.ae/register.aspx"
'Win API to maximize it.
'Visible prop not required anymore
'ShowWindow IE.hwnd, SW_SHOWMAXIMIZED '<--- Comment out if you want small IE window
'Wait till IE is completely ready/visible
Do While IE.readyState <> READYSTATE_COMPLETE: DoEvents: Loop
'Wait an extra 3 seconds
Application.Wait Now + TimeValue("0:00:03")
'Get the content (the HTML code) of page to variable htmlDoc
Set htmlDoc = IE.document
'Find Language (English)
Set htmlAs = htmlDoc.getElementsByTagName("a")
'. . . and click
For Each htmlA In htmlAs
If htmlA.innerText = "English" And htmlA.ID = "ucHeaderExternal_lnkBtnToggleLanguage" Then
htmlA.Click
Exit For
End If
Next htmlA
'Wait till IE is completely ready/visible
Do While IE.readyState <> READYSTATE_COMPLETE: DoEvents: Loop
'Wait an extra 5 seconds
Application.Wait Now + TimeValue("0:00:05")
'Loop through option elements
Set htmlOptions = htmlDoc.getElementsByTagName("option")
'Find and select Option "Unique Number"
For Each htmlOption In htmlOptions
If htmlOption.getAttribute("Value") = 9 Then
htmlOption.Selected = True
Exit For
End If
Next htmlOption
'Loop through input elements
Set htmlInputs = htmlDoc.getElementsByTagName("input")
'Find and fill in ID number
For Each htmlInput In htmlInputs
If htmlInput.ID = "PageContent_partyInfoControl_UCSelectCaseParty1_txtId" Then
htmlInput.Value = 35053392
Exit For
End If
Next htmlInput
'Loop through input elements
Set htmlInputs = htmlDoc.getElementsByTagName("input")
'Find and click "Retrieve Information from e-Gov"
For Each htmlInput In htmlInputs
If htmlInput.ID = "PageContent_partyInfoControl_UCSelectCaseParty1_btnRetrieveFromEgov" Then
htmlInput.Click
Exit For
End If
Next htmlInput
'Quit ALL instances of Internet Explorer
'Shell ("C:\Windows\system32\cmd.exe /c wmic process where name='iexplore.exe' call terminate")
End Sub
This is the HTML
HTML:
<div id="PageContent_partyInfoControl_UCSelectCaseParty1_pnlPersonInfo">
<div class="form-group">
<label class="col-md-2 control-label" for="PageContent_partyInfoControl_UCSelectCaseParty1_ddlIdType" id="PageContent_partyInfoControl_UCSelectCaseParty1_Label1">ID Type</label>
<div class="col-md-4">
<select class="form-control input-sm" id="PageContent_partyInfoControl_UCSelectCaseParty1_ddlIdType" name="ctl00$PageContent$partyInfoControl$UCSelectCaseParty1$ddlIdType" onchange="javascript:setTimeout('__doPostBack(\'ctl00$PageContent$partyInfoControl$UCSelectCaseParty1$ddlIdType\',\'\')', 0)">
<option selected="selected" value="2">
Residency Number
</option>
<option value="4">
Edbara Numeber
</option>
<option value="8">
Emirates ID Number
</option>
<option value="9">
Uniqe Number
</option>
<option value="100">
Passport No
</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label" for="PageContent_partyInfoControl_UCSelectCaseParty1_txtId" id="PageContent_partyInfoControl_UCSelectCaseParty1_Label2">ID</label>
<div class="col-md-4">
<input class="form-control-validation" id="PageContent_partyInfoControl_UCSelectCaseParty1_txtId" maxlength="20" name="ctl00$PageContent$partyInfoControl$UCSelectCaseParty1$txtId" type="text"> <span class="validation">*</span> <span class="text-danger" id="PageContent_partyInfoControl_UCSelectCaseParty1_rfvTxtId" style="visibility:hidden;">Required</span>
</div>
</div>
<div class="form-group">
<div class="col-md-2"></div>
<div class="col-md-4">
<input class="btn btn-primary top-buffer" id="PageContent_partyInfoControl_UCSelectCaseParty1_btnRetrieveFromEgov" name="ctl00$PageContent$partyInfoControl$UCSelectCaseParty1$btnRetrieveFromEgov" *******="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$PageContent$partyInfoControl$UCSelectCaseParty1$btnRetrieveFromEgov", "", true, "0aa16a88-c8ed-46d5-aeba-2732abb383ae", "", false, false))" type="submit" value="Retrieve Information from e-Gov">
</div>
</div>
</div>
Last edited: