Refresh or reload Internet Explorer object in Excel VBA - 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.

Related

VBA closing IE javascript popup

The below code opens an instance of InternetExplorer and downloads odds. It works fine but occasionally a pop-up window appears which causes the code to not work. Any help on how to navigate the below pop-up (i.e. click 'continue to oddschecker') when the pop-up does appear?
<a class="continue beta-callout js-close-class" onclick='s_objectID="javascript:void(0)_9";return this.s_oc?this.s_oc(e):true' href="javascript:void(0)">Continue to Oddschecker</a>
Full code:
Sub Oddschecker()
Dim ie, wp As Object
Dim i As Integer
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = False
ie.Navigate "https://www.oddschecker.com/horse-racing/racing-coupon"
Do While ie.Busy
DoEvents
Loop
Do While ie.ReadyState <> 4
DoEvents
Loop
Set wp = ie.Document
'Application.ActiveSheet.UsedRange.ClearContents
Application.Worksheets("sheet1").UsedRange.ClearContents
i = 2
For Each rw In wp.getElementsByTagName("table")(0).getElementsByTagName("tr")
If rw.className = "date" Then
Worksheets("sheet1").Range("A1") = rw.innerText
ElseIf rw.className = "fixture-name" Then
i = i + 1
Worksheets("sheet1").Range("A" & i) = rw.getElementsByTagName("td")(0).innerText
i = i + 1
ElseIf rw.className = "coupons-table-row match-on" Then
For Each od In rw.getElementsByTagName("p")
If InStr(od.innerText, "(") <> 0 Then
Worksheets("sheet1").Range("A" & i) = Trim(Left(od.innerText, InStr(od.innerText, "(") - 1))
np = Trim(Right(od.innerText, Len(od.innerText) - InStr(od.innerText, "(")))
Worksheets("sheet1").Range("B" & i) = Left(np, Len(np) - 1)
i = i + 1
Else
Worksheets("sheet1").Range("A" & i) = Trim(od.innerText)
i = i + 1
End If
Next od
End If
Next rw
ie.Quit
Range("A1:B" & i).WrapText = False
Columns("A:B").EntireColumn.AutoFit
Set wp = Nothing
Set ie = Nothing
End Sub
If you wish to continue with that page (navigating to that popup page), you can try like:
Dim HTML As HTMLDocument, addcheck As Object
While IE.Busy = True Or IE.readyState < 4: DoEvents: Wend ''(You can write it the way you feel comfortable)
Set HTML = IE.document ''place this line after the prevous line
Set addcheck = HTML.querySelector("#promo-modal a.continue")
If Not addcheck Is Nothing Then
addcheck.Click
End If
But, that is not a good idea cause it will lead you to some page where you might need to do some activity to get back on this data ridden page.
I suppose you should get rid of that popup blocker by ticking the cross button located on the top right area and continue to do what you are doing:
Dim HTML As HTMLDocument, addcheck As Object
While IE.Busy = True Or IE.readyState < 4: DoEvents: Wend ''(You can write it the way you feel comfortable)
Set HTML = IE.document ''place this line after the prevous line
Set addcheck = HTML.querySelector("#promo-modal span[title='Close")
If Not addcheck Is Nothing Then
addcheck.Click
End If
If I didn't understand what your intention was, do let me know. Thanks.

Using VBA to click a link

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

Combobox Option Value Has spaces How do i Call them through VBA from excel

Sub Sprint()
Dim IE As Object
Dim objelement As Object
Dim c As Integer
Dim LastRow, i, j As Integer
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.navigate "http://xxx"
'wait until first page loads
Do Until .readyState = 4
DoEvents
Loop
On Error Resume Next
Set sht = ThisWorkbook.Worksheets("TestData1")
LastRow = sht.Cells(sht.Rows.Count, "B").End(xlUp).Row
For j = 2 To LastRow
i = 1
If IE.Document.all.Item(i).innertext = "ÔÍÑ (ãîñ. ïîøëèíà)" Then
IE.Document.all.Item(i).Click
End If
IE.Visible = True
While IE.Busy
DoEvents 'wait until IE is done loading page.
Wend
With IE.Document
'text boxes
IE.Document.getElementById("txtSubscriberId").Value = sht.Cells(j, 9)
.all("txtSubscriberId").Value = sht.Cells(j, 9)
.all("btnPermValidateAddr").Click
.all("txtSubscriberId").Value = sht.Cells(j, 9)
'This piece is selected in web page drop down since option value has no
spaces
IE.Document.getElementById("ddlProdCd").Value = sht.Cells(j, 20)
IE.Document.getElementById("ddlProdCd").FireEvent ("onfocus")
IE.Document.getElementById("ddlProdCd").FireEvent ("onchange")
IE.Document.getElementById("ddlProdCd").FireEvent ("onmousewheel")
'This piece is not selected in web page drop down since option value has
spaces
IE.Document.getElementById("ddlPlanDesc").Value = sht.Cells(j, 21)
IE.Document.getElementById("ddlPlanDesc").FireEvent ("onfocus")
IE.Document.getElementById("ddlPlanDesc").FireEvent ("onmousewheel")
IE.Document.getElementById("ddlPlanDesc").FireEvent ("onchange")
End With
Set IE = Nothing
Next j
End With
End Sub
In short 21st column value is not selected in webpage but 20th value is selected since in webpage option value has no space for 20th column value but 21st Column has space in option value in webpage itself
Refer attached Image
https://i.stack.imgur.com/HwwKB.png

Crawler & Scraper using excel vba

I am trying to crawl in an intranet URL, so I can get the excel automatically select one of the options from a dropdown menu, then enter a value in a text box, then click on Find to get redirected to another page, where I want to get a value copy to another worksheet in the same workbook, I have created the below, but the code is not working, saying object required. :(
Sub Test()
Dim rng As Range
Set rng = Sheets("sheet1").Range("A1", Sheets("sheet1").Cells.Range("A1").End(xlDown))
Set ie = CreateObject("InternetExplorer.application")
ie.Visible = True
ie.Navigate ("https://gcd.ad.plc.cwintra.com/GCD_live/login/login.asp")
Do
If ie.ReadyState = 4 Then
ie.Visible = False
Exit Do
Else
DoEvents
End If
Loop
ie.Document.forms(0).all("txtUsername").Value = ""
ie.Document.forms(0).all("txtPassword").Value = ""
ie.Document.forms(0).submit
ie.Visible = True
Appliction.Wait (Now + TimeValue("00:00:02"))
DoEvents
For Each cell In rng
ie.Navigate ("https://gcd.ad.plc.cwintra.com/GCD_live/search.asp")
DoEvents
ie.Document.getElementById("cboFieldName").selectedIndex = 6
ie.Document.getElementById("txtFieldValue").Select
SendKeys (cell.Value)
DoEvents
ie.Document.getElementById("cmdFind").Click
Next cell
End Sub

VBA to find text from webpages

I have created Macro which gives me all URLs present on any webpages.
We just need to provide the URL and it gives us the all links present in that webpage and paste it in one column
Private Sub CommandButton4_Click()
'We refer to an active copy of Internet Explorer
Dim ie As InternetExplorer
'code to refer to the HTML document returned
Dim html As HTMLDocument
Dim ElementCol As Object
Dim Link As Object
Dim erow As Long
Application.ScreenUpdating = False
'open Internet Explorer and go to website
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate Cells(1, 1)
'Wait until IE is done loading page
Do While ie.READYSTATE <> READYSTATE_COMPLETE
Application.StatusBar = "Trying to go to website…"
DoEvents
Loop
Set html = ie.document
'Display text of HTML document returned in a cell
'Range("A1") = html.DocumentElement.innerHTML
Set ElementCol = html.getElementsByTagName("a")
For Each Link In ElementCol
erow = Worksheets("Sheet4").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Cells(erow, 1).Value = Link
Cells(erow, 1).Columns.AutoFit
Next
'close down IE, reset status bar & turn on screenupdating
'Set ie = Nothing
Application.StatusBar = ""
Application.ScreenUpdating = True
ie.Quit
ActiveSheet.Range("$A$1:$A$2752").removeDuplicates Columns:=1, Header:=xlNo
End Sub
Now can anyone will help me to create macro to find particular text from all these URLs present in column and if that text is present then in next column it should print text "text found".
Example if we search text "New" then it should print text "Text found" in next column of the URL.
Thank you.
The key would be the function Instr, if it finds the string "New", it returns the position where it begins, otherwise it returns 0.
i=1
do until trim(Cells(i,1).Value) = vbNullString
if instr(Cells(i,1).Value,"New") then
Cells(i,2).value="Text found"
end if
i=i+1
loop
Similar to above.
Dim a As Variant
a = 2
While Cells(a, 1) <> "" And Cells(a + 1, 1) <> ""
If InStr(Cells(a, 1), "new") = 0 Then
Else
Cells(a, 2) = "Text Found"
End If
a = a + 1
Wend