VBA: Click button on HTML form but it results in an error.

strooman

Active Member
Joined
Oct 29, 2013
Messages
333
Office Version
  1. 2016
Platform
  1. Windows
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:

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:

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Anybody with a suggestion?

I forgot to mention that this macro need references to:
Microsoft HTML.Object Library
Microsoft Internet Controls
Microsoft XML,v6.0
Accessible via: Alt+F11 | Tools | References
 
Upvote 0
Instead of clicking the button have you tried submitting the form?
Code:
htmlDoc.forms(0).submit
 
Upvote 0
Instead of clicking the button have you tried submitting the form?
Code:
htmlDoc.forms(0).submit

Thanks for your reply and suggestion Norie. Unfortunately it didn't work.
 
Upvote 0

Forum statistics

Threads
1,223,900
Messages
6,175,276
Members
452,629
Latest member
SahilPolekar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top