I'm trying to fill out a form using VBA (just to get more skilled in programming with VBA). I noticed that the web form is not updating a hidden field when a radio button is checked via VBA.
To be more specific, the hidden field doesn't become visible after checking the radio button.
In my research to why that is I read that: "Events won't (usually) fire when you change the data programmatically". When this assumption is true, what's the difference between filling out a form manually or programmatically? But when this assumption is wrong, what adjustments do I need to make in my code in order to make hidden fields visible after clicking that radio button?
The website I'm working with is from the Dutch Tax authorities: https://www.belastingdienst.nl/wps/wcm/connect/nl/auto-en-vervoer/content/hulpmiddel-motorrijtuigenbelasting-berekenen
This is my code.
Sub Show_Hidden_Field()
'Declaration section
Dim htmlDoc As New MSHTML.HTMLDocument
Dim objShell, objShellWindows, objShellWindow As Object
Dim htmlOption As MSHTML.IHTMLElement
'Wait an extra 3 seconds so you can switch to the IE window
'and see what's going on
Application.Wait Now + TimeValue("0:00:03")
'Find the correct Internet Explorer window
Set objShell = CreateObject("Shell.Application")
Set objShellWindows = objShell.Windows
For Each objShellWindow In objShellWindows
If objShellWindow.Name = "Internet Explorer" Then
'Find this window
If objShellWindow.LocationURL = "https://www.belastingdienst.nl/wps/wcm/connect/nl/auto-en-vervoer/content/hulpmiddel-motorrijtuigenbelasting-berekenen" Then
'This the correct page. Get the content of page
Set htmlDoc = objShellWindow.document
Exit For
End If
End If
Next objShellWindow
'Find the radio button . . .
Set htmlOption = htmlDoc.getElementById("V1-1_True")
'. . . and check it.
htmlOption.Checked = True
'**********************************************************************
'I've also tried these options without succes.
'htmlOption.Click
'htmlOption.FireEvent ("onchange")
'Or set the focus to another field
'**********************************************************************
End Sub
答案 0 :(得分:3)
You have a couple issues with your code. The first one is that you are using the wrong type for your radio button element.
While htmlOption
is technically a MSHTML.IHTMLElement
, it more specifically should be of type: MSHTML.HTMLInputElement
. So change your declaration to:
Dim htmlOption As MSHTML.HTMLInputElement
For your actual issue, you should just use .Click
. Taking a look at developer.mozilla.org:
The
HTMLElement.click()
method simulates a mouse click on an element.When
click()
is used with supported elements (such as an<input>
), it fires the element's click event. This event then bubbles up to elements higher in the document tree (or event chain) and fires their click events. (emphasis added)
So, you wouldn't need to use .Checked = True
, because upon clicking the input element, you will not only solve your problem with the hidden area being revealed, you are also simultaneously changing the value to True
.
So you would just use this:
'Find the radio button . . .
Set htmlOption = htmlDoc.getElementById("V1-1_True")
'. . . and check it.
htmlOption.Click