0

I wrote a macro so I could fill a form and access a website automatically:

Private Sub CommandButton1_Click()
Dim HTMLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer

Dim MyHTML_Element As IHTMLElement
Dim MyURL As String
On Error GoTo Err_Clear
MyURL = "EXAMPLE.COM"
Set MyBrowser = New InternetExplorer
MyBrowser.Silent = True
MyBrowser.navigate MyURL
MyBrowser.Visible = True
Do
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = MyBrowser.document
HTMLDoc.all.USER.Value = "USER1" 
HTMLDoc.all.USER.FireEvent ("onchange")
HTMLDoc.all.PASS.Value = "PASS"
HTMLDoc.all.PASS.FireEvent ("onchange")
For Each MyHTML_Element In HTMLDoc.getElementsByTagName("img")
  If MyHTML_Element.getAttribute("src") = "imagesrc.gif" Then MyHTML_Element.Click: Exit For
Next

Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub

After the page loads, I've been trying to get it to click another button with no luck. Do you have any idea for this?

12
  • Have you read the documentation? Jan 13, 2015 at 3:38
  • not enough of it i guess, will do @JohnSaunders
    – MarcosDrod
    Jan 13, 2015 at 3:49
  • 1
    You also didn't tell us what doesn't work, and this is either VBA or VB.NET, not both. Jan 13, 2015 at 3:51
  • From my experience, you need to be looking for the input tag associated with the button. Jan 13, 2015 at 3:52
  • it is VBA, it does work so far, im trying to add more code so it will click another button after the loggin loads @JohnSaunders
    – MarcosDrod
    Jan 13, 2015 at 3:54

1 Answer 1

0

Here is how to pause your program so that it can wait for the webpage to load. There are multiple ways of doing this but this little line of code works wonders.

Application.Wait(Now + TimeValue("0:00:10"))

Just copy and paste that into your code after the click even. This will wait for approximately 10 seconds. You can adjust the time as needed.

2
  • thanks for the suggest, tried this before but it does not work, even if i try to implement it before the application fills up the login form for example, it won't make it wait.
    – MarcosDrod
    Jan 13, 2015 at 5:19
  • anything bro? been reading all over the place, couldnt find a solution
    – MarcosDrod
    Jan 13, 2015 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.