Using VBA to click a link - vba

I'm trying next to click a link to view information on a page using VBA to then move on to edit that information with VBA, But trying to figure out how to write the code for it as the ID information changes with each search,
Any ideas on this?
I've looked around and can't seem to understand how to get VBA to pick this line up as the (ID=) and it isn't joined to the same ID as I'm searching for.
There is also serval references for
This is the line of code.
View
This is my current code to do the search for it. Without clicking on the view section yet.
Sub Test()
Dim ie As Object
Dim form As Variant
Dim button As Variant
Dim LR As Integer
Dim var As String
LR = Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To LR
var = Cells(x, 1).Value
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
With ie
.Visible = True
.navigate "*******"
While Not .readyState = READYSTATE_COMPLETE
Wend
End With
'Wait some to time for loading the page
While ie.Busy
DoEvents
Wend
Application.Wait (Now + TimeValue("0:00:02"))
ie.document.getElementById("quicksearch").Value = var
'code to click the button
Set form = ie.document.getElementsByTagName("form")
Application.Wait (Now + TimeValue("0:00:02"))
Set button = form(0).onsubmit
form(0).submit
'wait for page to load
While ie.Busy
DoEvents
Wend
Next x
End Sub
Edited
I've added in the code I think it should be following that link and a bit of tinkering to get it to not error with compiler errors :D, All seems to work but when it get's to the line to click the link it doesn't fail but doesn't even click it. It will then move on to the next one in the list in the spreed sheet, Which is expected.
Following it through with the debugger that shows nothing erroring or failing which is what I expect it to do if the code was wrong or link,
Any help, please ?
This is the code now
Sub Test1()
Dim ie As Object
Dim form As Variant
Dim button As Variant
Dim LR As Integer
Dim var As String
LR = Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To LR
var = Cells(x, 1).Value
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
Dim a
Dim linkhref
linkhref = "/?do_Action=ViewEntity&Entity_ID"
With ie
.Visible = True
.navigate "*******"
While Not .readyState = READYSTATE_COMPLETE
Wend
End With
'Wait some to time for loading the page
While ie.Busy
DoEvents
Wend
Application.Wait (Now + TimeValue("0:00:02"))
ie.document.getElementById("quicksearchbox").Value = var
'code to click the button
Set form = ie.document.getElementsByTagName("form")
Application.Wait (Now + TimeValue("0:00:02"))
Set button = form(0).onsubmit
form(0).submit
'wait for page to load
While ie.Busy
DoEvents
Wend
Application.Wait (Now + TimeValue("0:00:02"))
For Each a In ie.document.getElementsByTagName("a")
If (a.getAttribute("href")) = ("/?do_Action=ViewEntity&Entity_ID=") Then
a.Click
Exit For
Application.Wait (Now + TimeValue("0:00:02"))
While ie.Busy
DoEvents
Wend
End If
Next
Next x
End Sub
This is a copy of the code around the buttons,
This is the code surrounding the buttons,
View
Decom
Log</td>
Thank you.

Use a For to check every <a> tag element and make sure you click the right one. It's true your ID changes, but rest of string is constant, so that's 1 factor. Also, it looks like you always will click where it says View so that's another constant.
With both options, we can develop a simple For..Next that will check every <a> element and will check if those 2 options requirements are fulfilled:
For Each a In ie.document.getElementsByTagName("a")
If Left(a.href, 37) = "/?do_Action=ViewEntity&Entity_ID=" And a.innerText = "View" Then
a.Click
Exit For
Next a
Try it and let's see if this works for you.

If the element href is something like "/?do_Action=ViewEntity&Entity_ID=14287", this if statement is never going to evaluate to True:
(a.getAttribute("href")) = ("/?do_Action=ViewEntity&Entity_ID=")
So the element will never be clicked.
If you know the Entity_ID you want to click you can do:
Dim Entity_ID as Integer
Entity_ID = 14287
If (a.getAttribute("href")) = "/?do_Action=ViewEntity&Entity_ID=" & Cstr(myID) Then a.click
Otherwise just check if the element href contains that url:
If InStr(1, a.getAttribute("href"), linkhref) > 0 Then a.click
EDIT
Ok, using the HTML you posted I am able to access the specified a tag that you requested, by doing this
For Each ele In ie.document.getElementById("searchresults").getElementsByTagName("a")
If InStr(1, ele.href, "do_Action=ViewEntity") > 0 Then
MsgBox "The button is found!"
End If
Next

Related

Refresh or reload Internet Explorer object in Excel VBA

I'm working on a way to fill in online forms using data from an Excel Spreadsheet. I'm using the VBA Modules in Excel to do so.
The steps are as follows:
1) Navigate to page
2) Fill in details
3) Click "Continue" to continue to the next page
4) Fill in more details
5) Click "Save" to save the page
Here is the code:
Sub FillInternetForm()
'Header Start
Dim IE As Object
Dim ROW As Integer
Dim MAXROW As Integer
Set IE = CreateObject("InternetExplorer.Application")
MAXROW = 3
ROW = 2
'Header End
'Step 1 Navigate to the page
IE.Navigate *PAGE_1_URL*
IE.Visible = True
While IE.Busy Or IE.ReadyState <> 4
DoEvents
Wend
'Step 1 Ends
'Step 2&3 Fill in the details and click "continue"
IE.Document.ALL("ctl00_PageContent_PAYDPaymentMode").Value = "18"
IE.Document.ALL("ctl00_PageContent_PAYDPaymentDate").Value = Cells(ROW, 1).Value
IE.Document.ALL("ctl00_PageContent_PAYDBusinessUnit").Value = "104"
IE.Document.ALL("ctl00_PageContent_PAYDAmountPaid").Value = Cells(ROW, 2).Value
IE.Document.ALL("ctl00_PageContent_ContinueButton__Button").Click
While IE.Busy Or IE.ReadyState <> 4
DoEvents
Wend
'Step 2&3 Ends (IE moved to page 2)
'Step 4&5 Fill in more details and click "Save"
IE.Document.ALL("ctl00_PageContent_BankChequeOrOtherRef").Value = ThisWorkbook.Sheets("sheet1").Cells(ROW, 3).Value
IE.Document.ALL("ctl00_PageContent_PAYDRemarks").Value = ThisWorkbook.Sheets("sheet1").Cells(ROW, 4).Value
IE.Document.ALL("ctl00_PageContent_SaveButton__Button").Click
'Step 4&5 Ends
End Sub
I did step 1 to 3 fine. However, when it comes to step 4, I get an error. The error is: Runtime error 424 Object required. I am aware that this means that the Module is unable to locate the elements in step 4.
The error is at exactly:
IE.Document.ALL("ctl00_PageContent_BankChequeOrOtherRef").Value = ThisWorkbook.Sheets("sheet1").Cells(ROW, 3).Value
Before the process has to be terminated.
To debug whether the code in step 4 has any problem, I took it out and ran it separately. I took the entire section of Step4&5 and header out and make it navigate to the 2nd page instead.
Like so:
Sub FillInternetForm()
Dim IE As Object
Dim ROW As Integer
Dim MAXROW As Integer
Set IE = CreateObject("InternetExplorer.Application")
MAXROW = 3
ROW = 2
IE.Navigate *PAGE_2_URL*
IE.Visible = True
While IE.Busy Or IE.ReadyState <> 4
DoEvents 'wait until IE is done loading page.
Wend
IE.Document.ALL("ctl00_PageContent_BankChequeOrOtherRef").Value = ThisWorkbook.Sheets("sheet1").Cells(ROW, 3).Value
IE.Document.ALL("ctl00_PageContent_PAYDRemarks").Value = ThisWorkbook.Sheets("sheet1").Cells(ROW, 4).Value
IE.Document.ALL("ctl00_PageContent_SaveButton__Button").Click
End Sub
And it worked.
I have no idea what is the problem. I'm guessing that all the elementID was loaded at the start or something and that the page 2 IDs were not loaded?
Does anyone know the source or solution to this problem? Thank you.
P.S. All names element ID are copied and pasted from the source code of the webform and should be correct.
Edit:
I have updated the issue in a comment below.
Edit2: Tried the exact same code on a win10 machine with excel 2016. Code worked perfectly. Thanks for all the help provided.
Please give this a try... (not tested)
Sub FillInternetForm()
'Header Start
Dim IE As Object
Dim Doc As Object
Dim ROW As Integer
Dim MAXROW As Integer
Dim ContinueButton As Object
Dim SaveButton As Object
Set IE = CreateObject("InternetExplorer.Application")
MAXROW = 3
ROW = 2
'Header End
'Step 1 Navigate to the page
IE.Navigate PAGE_1_URL
IE.Visible = True
While IE.Busy Or IE.ReadyState <> 4 Or ContinueButton Is Nothing
DoEvents
Set Doc = IE.document
Set ContinueButton = Doc.ALL("ctl00_PageContent_ContinueButton__Button")
Wend
'Step 1 Ends
'Step 2&3 Fill in the details and click "continue"
Doc.ALL("ctl00_PageContent_PAYDPaymentMode").Value = "18"
Doc.ALL("ctl00_PageContent_PAYDPaymentDate").Value = Cells(ROW, 1).Value
Doc.ALL("ctl00_PageContent_PAYDBusinessUnit").Value = "104"
Doc.ALL("ctl00_PageContent_PAYDAmountPaid").Value = Cells(ROW, 2).Value
ContinueButton.Click
While IE.Busy Or IE.ReadyState <> 4 Or SaveButton Is Nothing
DoEvents
Set Doc = IE.document
Set SaveButton = Doc.ALL("ctl00_PageContent_SaveButton__Button")
Wend
'Step 2&3 Ends (IE moved to page 2)
'Step 4&5 Fill in more details and click "Save"
Doc.ALL("ctl00_PageContent_BankChequeOrOtherRef").Value = ThisWorkbook.Sheets("sheet1").Cells(ROW, 3).Value
Doc.ALL("ctl00_PageContent_PAYDRemarks").Value = ThisWorkbook.Sheets("sheet1").Cells(ROW, 4).Value
SaveButton.Click
'Step 4&5 Ends
End Sub
Updated the code to:
Sub FillInternetForm()
'Header Start
Dim IE As Object
Dim DOC As Object
Dim ROW As Integer
Dim MAXROW As Integer
Dim SAVEBUTTON As Object
Dim COUNTER As Integer
Set IE = CreateObject("InternetExplorer.Application")
Set SAVEBUTTON = Nothing
MAXROW = 3
ROW = 2
COUNTER = 0
'Header End
'Step 1 Navigate to the page
IE.Navigate "*WEBSITE*"
IE.Visible = True
While IE.Busy Or IE.ReadyState <> 4
DoEvents
Wend
'Step 1 Ends
Set DOC = IE.Document
'Step 2&3 Fill in the details and click "continue"
DOC.ALL("ctl00_PageContent_PAYDPaymentMode").Value = "18"
DOC.ALL("ctl00_PageContent_PAYDPaymentDate").Value = Cells(ROW, 1).Value
DOC.ALL("ctl00_PageContent_PAYDBusinessUnit").Value = "104"
DOC.ALL("ctl00_PageContent_PAYDAmountPaid").Value = Cells(ROW, 2).Value
DOC.ALL("ctl00_PageContent_ContinueButton__Button").Click
'Step 2&3 Ends (IE moved to page 2)
While IE.Busy Or IE.ReadyState <> 4 Or SAVEBUTTON Is Nothing
DoEvents
If Not IE.Busy Or IE.ReadyState = 4 Then
IE.Refresh
End If
Set DOC = IE.Document
On Error Resume Next
Set SAVEBUTTON = DOC.ALL("ctl00_PageContent_SaveButton__Button")
COUNTER = COUNTER + 1
Wend
'Step 4&5 Fill in more details and click "Save"
DOC.ALL("ctl00_PageContent_BankChequeOrOtherRef").Value = ThisWorkbook.Sheets("sheet1").Cells(ROW, 3).Value
DOC.ALL("ctl00_PageContent_PAYDRemarks").Value = ThisWorkbook.Sheets("sheet1").Cells(ROW, 4).Value
DOC.ALL("ctl00_PageContent_SaveButton__Button").Click
'Step 4&5 Ends
End Sub
Found out that this snippet is not refreshing the page even though it is supposed to.
If Not IE.Busy Or IE.ReadyState = 4 Then
IE.Refresh
End If
Is it possible that the object is dropped halfway?
Occasionally the code works when visibility of IE is turned off, but it works like once every hour or something before the exact same error pops up.
The page I am accessing seems to use ASPX and generates the page base on a database, no idea if this will help.
Edit: Tried the exact same code on a win10 machine with excel 2016. Code worked perfectly.

Excel VBA multiple For loop not working

I seem to be stuck in an weird issue. I have the Excel VBA code below to visit a website and enter a userID (from the userID column A in sheet 1) and retrieve the name of the user which shows up after hitting the submit button then continues with the rest of the userIDs.
Public Sub TEST()
TestPage = "http://test.com/"
Dim IE As New InternetExplorer
Dim Doc As HTMLDocument
Dim GetElem As Object
Dim GetElem2 As Object
IE.Visible = True
IE.navigate TestPage
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Application.Wait (Now + TimeValue("0:00:04"))
Set Doc = IE.document
CurRow = Sheet1.UsedRange.Rows.Count
Do While CurRow > 0
Application.Wait (Now + TimeValue("0:00:4"))
'Find/Get the userID textbox and enter the current userID
For Each GetElem In Doc.getElementsByTagName("input")
If GetElem.ID = "query" Then 'I could just do getElementByID later
GetElem.Value = Sheet1.Range("A" & CurRow).Value
End If
Next
'Find and click the submit button
For Each GetElem2 In Doc.getElementsByTagName("button")
If GetElem2.Type = "submit" Then
GetElem2.Click
Application.Wait (Now + TimeValue("0:00:03"))
End If
Next
CurRow = CurRow - 1
Loop
End Sub
The problem is the code works only once. It enters the first userID into the text box and hits Submit. When it loops and tries to enter the next userID though, the code get stuck in a loop.
If I remove the entire 2nd For-Next loop it works (although it doesn't submit, it enters each of the userIDs in the text box).
On top of that, if I use the F8 debugging the code step by step, everything works fine. Only getting problems when fully running the code.:(
Public Sub TEST()
TestPage = "http://test.com/"
Dim IE As New InternetExplorer
Dim Doc As HTMLDocument
Dim GetElem As Object
Dim GetElem2 As Object
IE.Visible = True
IE.navigate TestPage
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Application.Wait (Now + TimeValue("0:00:04"))
Set Doc = IE.document
CurRow = Sheet1.UsedRange.Rows.Count
For Each GetElem In Doc.getElementsByTagName("input")
If GetElem.ID = "query" Then 'I could just do getElementByID later
GetElem.Value = Sheet1.Range("A" & CurRow).Value
For Each GetElem2 In Doc.getElementsByTagName("button")
If GetElem2.Type = "submit" Then
GetElem2.Click
Application.Wait (Now + TimeValue("0:00:03"))
End If
Next GetElem2
End If
CurRow = CurRow + 1
Next GetElem
End Sub

VBA Internet Explorer Application gives different results for each function call

I'm trying to automate a task in excel that requires opening a webpage, navigating to a link on that page, and then clicking on a button on the second page to download an .xlsx file.
I've written a script that should do this. However, the response I get from the webpage is not always the same. In particular, sometimes this will return a download from the first page and sometimes it will navigate to the second page and not download anything, once or twice it has done both.
My sense is that this has to do with how long it takes for InternetExplorer.application to complete a request. I can't figure out how to troubleshoot this though, given that I tell the script to wait for IE.application to complete its request.
Sub DoBrowse2()
'For Each lnk In Sheets("Sheet4").Hyperlinks
'Range(lnk).Hy.Follow
'Next
Dim i As Long
Dim URL As String
Dim BaseURL As String
Dim ToURL As String
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
Dim HWNDSrc As Long
Dim html As IHTMLDocument
Set IE = CreateObject("InternetExplorer.Application")
URL = Range("B2").Hyperlinks(1).Address
IE.Navigate URL
IE.Visible = True
Application.StatusBar = URL & " is loading. Please wait..."
Do While IE.ReadyState = 4: DoEvents: Loop
Do Until IE.ReadyState = 4: DoEvents: Loop
Application.StatusBar = URL & " Loaded"
'Set html = IE.Document
'Dim elements As IHTMLElementCollection
'Set elements = html.all
For Each itm In IE.Document.all
If itm.className = "datagrid" Then
For Each el In itm.Document.all
Debug.Print "hello"
If el.className = "ujump" And Right(el.innerText, 12) = "Constituents" Then
'Debug.Print el.innerText
ToURL = el.getAttribute("data-subset")
BaseURL = "http://datastream.thomsonreuters.com/navigator/search.aspx?dsid=ZUCH002&AppGroup=DSAddin&host=Metadata&prev=scmTELCMBR&s=D&subset="
ToURL = BaseURL & ToURL
'Debug.Print ToURL
IE.Navigate ToURL
IE.Visible = True
Do While IE.Busy
Debug.Print "in busy loop"
Application.Wait DateAdd("s", 1, Now)
Loop
GoTo end_of_for
End If
Next
End If
Next
end_of_for:
Debug.Print ("STOP STOP STOP STOP STOP")
Dim Script As String
For Each itm In IE.Document.all
If itm.className = "lgc excel" Then
Debug.Print "hello world"
Debug.Print itm.getAttribute("onclick")
itm.Click
Do While IE.Busy
Debug.Print "app busy"
Application.Wait DateAdd("s", 1, Now)
Loop
Exit For
End If
Next
End Sub
Thanks in advance for your help.
Use this to determine whether IE page has been fully loaded, it always must be both of these conditions:
Do Until ie.ReadyState = 4 And ie.Busy = False
DoEvents
Loop
Even with code above if there are scripts on the page, some content may be loaded after ie.ReadyState = 4 And ie.Busy = False condition is met and either easy way, but inefficient and unreliable Application.Wait can be used or you can try finding elements on the website which inform about loading state and determine the state by their visible attributes etc.
Part of your code is wrong and causes an endless loop:
Do While IE.ReadyState = 4: DoEvents: Loop
Do Until IE.ReadyState = 4: DoEvents: Loop
It makes DoEvents fire while readystate is complete and also until it reaches complete status.
Narrow down a collection of all elements:
For Each itm In IE.Document.all
to a specific collection for better performance when possible, for example:
For Each itm In IE.Document.GetElementsByTagName("div")

Excel VBA to Enter Data Online With IE and Loop Through Rows

I'm trying to use Excel VBA to pull the info from columns A2-D2 and enter it into the web site and then click the "Next" button. The code below is what I have so far which works fine for entering the info found on row 2 only.
I'm hoping to achieve that IE opens a new window, enters the values in cells A2 through D2, clicks the "Next" button, and then loops to open another new IE window and enters the values in cells A3 through D3 until it hits an empty cell.
Here are the current numbers that I'm using for testing, http://imgur.com/a/88XEF.
Thanks in advance for any suggestions.
Sub FillInternetForm()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
'create new instance of IE. use reference to return current open IE if
'you want to use open IE window. Easiest way I know of is via title bar.
IE.Navigate "https://mygift.giftcardmall.com/Card/Login?returnURL=Transactions"
'go to web page listed inside quotes
IE.Visible = True
While IE.busy
DoEvents 'wait until IE is done loading page.
Wend
'pause if needed
Application.Wait Now + TimeValue("00:00:02")
IE.Document.All("CardNumber").Value = ThisWorkbook.Sheets("sheet1").Range("a2")
IE.Document.All("ExpirationMonth").Value = ThisWorkbook.Sheets("sheet1").Range("b2")
IE.Document.All("ExpirationYear").Value = ThisWorkbook.Sheets("sheet1").Range("c2")
IE.Document.All("SecurityCode").Value = ThisWorkbook.Sheets("sheet1").Range("d2")
'presses the next button
Set tags = IE.Document.GetElementsByTagname("Input")
For Each tagx In tags
If tagx.Value = "Next" Then
tagx.Click
Exit For
End If
Next
End Sub
You just need to wrap your code in a loop..
Dim i as integer
i = 2
do while (ThisWorkbook.Sheets("sheet1").cells(i, 1).value <> "")
'your code from Application.wait line to end of next button click
i = i + 1
loop
This assumes an empty row can be identified by column A being empty. You could change the condition on the while loop if this assumption is bad
sorry had to right a new answer because the formatting was going weird
ok makes sense, you just need to move the start of your loop further up the code so it encases the creation of the IE object and the navigation, you should also close the IE window before opening a new one:
move the chunk:
Dim i as integer
i = 2
do while (ThisWorkbook.Sheets("sheet1").cells(i, 1).value <> "")
right up to the top right after:
Sub FillInternetForm()
Add the following lines after the line "Next" right at the bottom but still enclosed by the loop
IE.Quit
Set IE = Nothing
I was able to accomplish what I wanted with the following. Thanks to those that commented.
Sub FillInternetForm()
Range("A2").Select
Do Until IsEmpty(ActiveCell)
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
'create new instance of IE. use reference to return current open IE if
'you want to use open IE window. Easiest way I know of is via title bar.
IE.Navigate "https://mygift.giftcardmall.com/Card/Login?returnURL=Transactions"
'go to web page listed inside quotes
IE.Visible = True
While IE.busy
DoEvents 'wait until IE is done loading page.
Wend
'pause if needed
Application.Wait Now + TimeValue("00:00:02")
IE.Document.All("CardNumber").Value = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
IE.Document.All("ExpirationMonth").Value = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
IE.Document.All("ExpirationYear").Value = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
IE.Document.All("SecurityCode").Value = ActiveCell.Value
ActiveCell.Offset(1, -3).Select
'presses the next button
Set tags = IE.Document.GetElementsByTagname("Input")
For Each tagx In tags
If tagx.Value = "Next" Then
tagx.Click
Exit For
End If
Next
Loop
End Sub

Webbrowser control in userform: how to wait for page to initialize

Task: using Excel VBA to navigate to a website, log in and go to an input page.
On that page, sequentially enter a series of values stored in a column in Sheet1.
What I've done so far:
I create a webbrowser control and navigate to the website and stop.
Then click on a button on Sheet1 with the macros that will do the inputting, stored in a module.
What's happening:
The control comes up nicely and navigates to the intended site. (this is the userform code)
Click on the button and it gets the userid and password from the spreadsheet, inputs them, clicks on the login button and all is well.
However, the next statement is:
Set inputfield = WebBrowser.objWebBrowser.Document.getElementById("ctl02_ctl03_ddlBus")
and inputfield comes up empty.
If I stop execution and step through it, it'll work.
I've tried Application.Wait; For x = 1 to 5000000; On Error Goto/Resume and keep trying, but nothings works.
I've also tried .NavigateComplete, .DocumentCompleted, as well as others, but I get errors saying member is not supported.
I am at my wits end - I'm just so close!! So far, I've spent more time on this that it will ever save, but now it's personal! Thanks for your help.
This is borrowed code from another site that initializes the control.
Private Sub UserForm_Initialize()
Dim a, c As Integer
With Me
.StartUpPosition = 0
.Top = 150
.Left = -700
End With
With Me.objWebBrowser
.Navigate2 "http://www.schoolbuscity.com/Mapnetweb_47/login.aspx"
.Visible = True
End With
End Sub
Private Sub GetSheets()
'this is my code
Dim inputfield As Object
Dim SendText As String
Dim NumberOfRoutes, r, errCount As Integer
errCount = 0
NumberOfRoutes = Range("NumberOfRoutes")
ReDim RouteNumbers(NumberOfRoutes) As String
For r = 1 To NumberOfRoutes
RouteNumbers(r) = Cells(r, 1).Value
Next r
' Sheets("Sheet1").Select
Range(Cells(5, 2), Cells(6, 2)).ClearContents ' this indicates success for the chosen cells
SendText = Range("userid").Value
Set inputfield = WebBrowser.objWebBrowser.Document.getElementById("Login1_UserName")
inputfield.Value = SendText
SendText = Range("password").Value
Set inputfield = WebBrowser.objWebBrowser.Document.getElementById("Login1_Password")
inputfield.Value = SendText
Set inputfield = WebBrowser.objWebBrowser.Document.getElementById("Login1_Login")
Application.Wait (Now + TimeValue("0:00:01"))
inputfield.Click
Application.Wait (Now + TimeValue("0:00:01")) ' I've tried waiting for up to 10 seconds
Set inputfield = Nothing
On Error GoTo TryAgain
For r = 5 To 6 'NumberOfRoutes ' just want to use 2 loops for testing
' this is where is fails, I believe, because the page is not initialized
' but if waiting is not the answer, then what is?
Set inputfield = WebBrowser.objWebBrowser.Document.getElementById("ctl02_ctl03_ddlBus")
inputfield.Value = RouteNumbers(r)
Set inputfield = WebBrowser.objWebBrowser.Document.getElementById("ctl02_ctl03_btnGo")
inputfield.Click
Application.Wait (Now + TimeValue("0:00:01"))
Cells(r, 2).Value = "Sent"
' WebBrowser.objWebBrowser.Document.Print
WebBrowser.objWebBrowser.GoBack
Next r
GoTo EndIt
TryAgain:
Set inputfield = Nothing
Set inputfield = WebBrowser.objWebBrowser.Document.getElementById("ctl02_ctl03_ddlBus")
errCount = errCount + 1
If errCount > 5 Then GoTo EndIt
Resume
EndIt:
If errCount > 0 Then
MsgBox "errCount= " + CStr(errCount)
Else
MsgBox "Did it"
End If
End Sub
This is how to waitbin vbscript.
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = 0
'must navigate to a local file to avoid security prompts
ie.Navigate2 "C:\Users\User\Desktop\Filter.html"
Do
wscript.sleep 100
Loop until ie.document.readystate = "complete"
examples in VBA:
Private Sub UserForm_Initialize()
Set ie = Me.WebBrowser1
ie.Navigate2 "about:blank"
Do Until ie.ReadyState = READYSTATE_COMPLETE
DoEvents
Loop
Set ie = Nothing
End Sub
Private Sub Conectar_Click()
Dim ie As Object
Set ie = Me.WebBrowser1
ie.Navigate2 "http://www.mytest.com"
Do Until ie.ReadyState = READYSTATE_COMPLETE
DoEvents
Loop
'different alternatives
'Dim inputfield As Object
'Set inputfield = ie.Document.getElementById("Login_tbLogin")
'inputfield.Value = "mylogin"
'Set inputfield = Nothing
'ie.Document.getElementById("Login_tbLogin").Value = "mylogin"
'ie.Document.All("Login_tbLogin").Focus
'ie.Document.All("Login_tbLogin").Value = "mylogin"
ie.Document.All.Item("Login_tbLogin").Value = "mylogin"
Set ie = Nothing
End Sub