PDA

View Full Version : Cant get the macro to click a button



pulkit lila
09-15-2012, 12:18 AM
i have made a macro to get into a site input a criteria in the search box but cant get it to click the search button.plz advise

CA Pulkit lila

Teeroy
09-15-2012, 07:01 PM
It was a bit trickier than I first thought but another forum (link below) helped. You code with the required changes is below. You need to set a reference to the Microsoft HTML Object Library.

Private Sub CommandButton1_Click()
Dim htmlInput As MSHTML.HTMLInputElement
Dim htmlColl As MSHTML.IHTMLElementCollection

If Range("a1").Value = 1 Then

Const BetJBN As String = "http://tracking.mscgva.ch/msctracking.php"

UserName = Range("b5").Value
Password = Range("b6").Value

On Error GoTo Err_Login

Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True

With ie
.navigate BetJBN 'address of log in page
Do While .busy: DoEvents: Loop
Do While .READYSTATE <> 4: DoEvents: Loop


'this information is specific to the web page
With .document.forms(0)
.InputBox.Value = "trlu1234567"
Set htmlColl = .getElementsByTagName("input")
Do While .READYSTATE <> "complete": DoEvents: Loop
For Each htmlInput In htmlColl
If Trim(htmlInput.Type) = "submit" Then
htmlInput.Click
Exit For
End If
Next htmlInput


End With
End With

Exit_Login:
Exit Sub
Err_Login:
MsgBox Err.Number & " " & Err.Description, vbOKOnly + vbCritical, "Error"
Resume Exit_Login

End If

End Sub

source: http://www.ozgrid.com/forum/showthread.php?t=138829