dimanche 21 juin 2015

Uncheck a checkbox in IE using VBA

I am trying to uncheck an already checked checkbox on a webpage using excel VBA.

HTML code:

    <input name=<name> class=<class> id=<id> type="checkbox" checked="checked">

VBA code stub:

    Public Sub uncheck(IE As Object)
    Dim Element As Object
        With IE.Document
            Set Element = .getElementById(<id>)
            Element.Checked = False
            Element.defaultChecked = False        
        End With
    End Sub

This does not work out as desired as HTML code changes to -

    <input name=<name> class=<class> id=<id> type="checkbox" checked="">

The checkbox is not unchecked as there is still the checked attribute

Even tried the below(Clicking on the checkbox). It does not do anything at all -

    Public Sub uncheck(IE As Object)
    Dim Element As Object
        With IE.Document
            Set Element = .getElementById(<id>)
            Element.Click
        End With
    End Sub

Any suggestions? Or may I know why my approach not working?




Aucun commentaire:

Enregistrer un commentaire