Excel VBA Code Not Working With Internet Explorer 11 - html

I have a vba code which can upload the data from an excel sheet to a website. However, the code works fine in IE browser 8,but it does not work on a win8 IE browser 11. It gives error 438 ("Object Does'nt support this property or method") and text field remains blank. Here are part of the code:
Dim ie As New InternetExplorer
Dim DOCS As HTMLDocument
ie.Navigate "http://uhs.edu.pk/results/etr2015.php"
ie.Visible = True
Do
DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE
ie.Document.getElementsName("ROLLNO").Value = "55"
Need help to resolve this problem.

This Should Work for you.
Sub testIE()
Dim ie As Object
Dim objCollection As Object
Dim i As Integer
'Create InternetExplorer Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
'Load the login page
ie.navigate "http://uhs.edu.pk/results/etr2015.php"
'Wait until the page is ready
Do While ie.busy
Application.Wait DateAdd("s", 1, Now)
Loop
'Get all the elements with input tag name
Set objCollection = ie.Document.getElementsByTagName("input")
i = 0
'Loop through all elements and find login form and fill it
While i < objCollection.Length
'Login name
If objCollection(i).Name = "rollno" Then
objCollection(i).Value = "55"
End If
i = i + 1
Wend
'Clean up
Set ie = Nothing
End Sub
If you also want to automate the serach button this should do the trick
Sub testIE()
Dim ie As Object
Dim objCollection As Object
Dim objElement As Object
Dim i As Integer
'Create InternetExplorer Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
'Load the login page
ie.navigate "http://uhs.edu.pk/results/etr2015.php"
'Wait until the page is ready
Do While ie.busy
Application.Wait DateAdd("s", 1, Now)
Loop
'Get all the elements with input tag name
Set objCollection = ie.Document.getElementsByTagName("input")
i = 0
'Loop through all elements and find login form and fill it
While i < objCollection.Length
'Login name
If objCollection(i).Name = "rollno" Then
objCollection(i).Value = "55"
End If
'Store login button in object
If objCollection(i).Type = "submit" Then
Set objElement = objCollection(i)
End If
i = i + 1
Wend
'Click login
objElement.Click
'Clean up
Set ie = Nothing
End Sub
Code to also fetch data from new page
Sub testIE()
Dim ie As Object
Dim objCollection As Object
Dim objElement As Object
Dim i As Integer
'Create InternetExplorer Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = False
'Load the login page
ie.navigate "http://uhs.edu.pk/results/etr2015.php"
'Wait until the page is ready
Do While ie.busy
Application.Wait DateAdd("s", 1, Now)
Loop
'Get all the elements with input tag name
Set objCollection = ie.Document.getElementsByTagName("input")
i = 0
'Loop through all elements and find login form and fill it
While i < objCollection.Length
'Login name
If objCollection(i).Name = "rollno" Then
objCollection(i).Value = "55"
End If
'Store login button in object
If objCollection(i).Type = "submit" Then
Set objElement = objCollection(i)
End If
i = i + 1
Wend
'Click login
objElement.Click
Do While ie.busy
Application.Wait DateAdd("s", 1, Now)
Loop
Set objCollection = ie.Document.getElementsByTagName("tr")
Range("A2").Value = Split(objCollection(23).innertext, "Sr. No.")(1)
Range("B2").Value = Split(objCollection(24).innertext, "Roll No.")(1)
Range("C2").Value = Split(objCollection(25).innertext, "Name of the Candidate")(1)
Range("D2").Value = Split(objCollection(26).innertext, "Father's Name ")(1)
Range("E2").Value = Split(objCollection(27).innertext, "Centre")(1)
Range("F2").Value = Split(objCollection(28).innertext, "Entrance Test Marks ")(1)
Range("G2").Value = Split(objCollection(29).innertext, "Total Marks")(1)
'Clean up
ie.Quit
Set ie = Nothing
End Sub
Hope this helps you out :)
Solution for combobox on the other page.
Sub testIE()
Dim ie As Object
Dim objCollection As Object
Dim objElement As Object
Dim i As Integer
'Create InternetExplorer Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
'Load the login page
ie.navigate "http://result.biselahore.com/"
'Wait until the page is ready
Do While ie.busy
Application.Wait DateAdd("s", 1, Now)
Loop
'Get all the elements with input tag name
Set objCollection = ie.Document.getElementsByTagName("Select")
i = 0
'Loop through all elements and find login form and fill it
While i < objCollection.Length
'either one of the methodes below works
'If objCollection(i).Name = "session" Then objCollection(i).Value = 2
If objCollection(i).Name = "session" Then objCollection(i).Item(2).Selected = True
If objCollection(i).Name = "year" Then objCollection(i).Item(2).Selected = True
i = i + 1
Wend
'Clean up
Set ie = Nothing
End Sub

Related

Excel VBA webpage dropdown list activation [duplicate]

I have a little problem with a VBA and HTML routine. I have to select from the link https://www.betexplorer.com/next/soccer/ the "Sort by:" drop-down menu and select the "Leagues" item. I can't do this via the VBA.
This is the code that I wrote
Sub Scarica()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLDoc1 As MSHTML.HTMLDocument
Dim Dropdowns As MSHTML.IHTMLElement
Dim post As MSHTML.IHTMLElement
Dim Elem As MSHTML.IHTMLElement
Application.StatusBar = "Download Elenco Campionati odierni in corso..."
Application.ScreenUpdating = False
Application.Calculation = xlManual
IE.Visible = True
IE.navigate "https://www.betexplorer.com/next/soccer/"
Do While IE.readyState <> READYSTATE_COMPLETE
Loop
'==================================================
' THIS IS THE PART THAT I CAN'T WRITE
'==================================================
Set HTMLDoc = IE.document
Set post = HTMLDoc.getElementById("wrap-header__list__item.semilong")
Do While IE.readyState <> READYSTATE_COMPLETE
Loop
'==================================================
'==================================================
This is my first message and I'm not able to insert the HTML code of the website.
You can isolate the correct dropdown item for selection by combining the id of the parent select with the attribute = value for the value attribute of the appropriate child option tag. The parent select is expecting an onchange event which you need to attach and dispatch.
Parent div by id and child option by value:
Event handler:
Option Explicit
Public Sub test()
Dim ie As InternetExplorer, evt As Object
Set ie = New InternetExplorer
With ie
.Visible = True
.Navigate2 "https://www.betexplorer.com/next/soccer/"
While .Busy Or .readyState <> 4: DoEvents: Wend
.document.querySelector("#nr-all [value='2']").Selected = True
Set evt = .document.createEvent("HTMLEvents")
evt.initEvent "change", True, False
.document.querySelector("#nr-all select").dispatchEvent evt
Stop '< delete me later
.Quit
End With
End Sub
The way #QHarr explained is just perfect. Or you can also do something like this with your existing code.
IE.Visible = True
IE.navigate "https://www.betexplorer.com/next/soccer/"
Do While IE.readyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.document
With HTMLDoc.getElementById("next-filter-sort").all(1).all(0)
.Focus
.Value = "2" '"Leagues"
End With
Sorry both codes work, but how come when I try to extrapolate the name of each league, those are loaded by the "Kick off times" and not by Leagues?
he code that I used is:
With IE
.Visible = True
.navigate "https://www.betexplorer.com/next/soccer/"
Do While .readyState <> READYSTATE_COMPLETE
Loop
.document.querySelector("#nr-all [value='2']").Selected = True
Set evt = .document.createEvent("HTMLEvents")
evt.initEvent "change", True, False
.document.querySelector("#nr-all select").dispatchEvent evt
Do While .readyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.document
End With
i = 9 'Riga di inizio copia dati
j = 0 'Colonna di inizio copia dati
Range("A10:A1005").ClearContents 'Pulisce la Zona dove saranno incollati i dati
Set mycoll = HTMLDoc.getElementsByTagName("TABLE")
For Each myItm In mycoll
For Each trtr In myItm.Rows
If trtr.classname = "js-tournament" Then
inizio = InStr(trtr.innerHTML, "href=") + 6
fine = InStr(trtr.innerHTML, "><i") - 1
fedhtml = Trim(Mid(trtr.innerHTML, inizio, fine - inizio))
campionato = Split(Replace(fedhtml, "/soccer/", ""), "/")
campionato = Trim(campionato(1))
Cells(i + 1, j + 1) = trtr.innerText
Cells(i + 1, j + 1).Select
Selection.RowHeight = 15
i = i + 1
End If
Next trtr
Next myItm

Entering text into webpage searchbox

I am trying to enter text into a searchbox and am running into different errors. Below is my code, can anyone point out to me where i have gone wrong?
Sub GetHTMLDocument()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
IE.Visible = True
IE.navigate "http://shopee.sg"
Do While IE.readyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.document
Set HTMLInput = HTMLDoc.getElementsByClassName("shopee-searchbar-input__input")
HTMLInput.Value = "Excel VBA"
End Sub
I suggest try to make a test with the code sample below may help to solve the issue.
Sub demo()
Dim URL As String
Dim IE As Object
Dim element As HTMLInputElement
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
URL = "YOUR WEB PAGE ADDRESS HERE" 'Add your site address here....
IE.navigate URL
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Application.Wait DateAdd("s", 2, Now)
Set element = IE.document.querySelector("[class='shopee-searchbar-input__input']")
element.removeAttribute ("aria-label")
element.removeAttribute ("placeholder")
element.Value = "abc"
element.FireEvent ("OnChange")
Set IE = Nothing
End Sub
Output:
Further, you can modify the code example as your requirements.

Press Login Button Excel Vba

I have the code below to login into the website in the code. However, the login button is not being clicked. I am new to Excel VBA, what do you suggest, please?
Dim HTMLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Sub Myenter()
Dim MyHTML_Element As IHTMLElement
Dim MyURL As String
On Error GoTo Err_Clear
MyURL = "https://willemendrees.nl/fn/login/"
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.id_username.Value = "amsterdam#willemendrees.nl"
HTMLDoc.all.id_password.Value = "*****"
For Each MyHTML_Element In HTMLDoc.getElementsByTagName("input")
If MyHTML_Element.Type = "submit" Then MyHTML_Element.Click: Exit For
Next
End With
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
You can use a CSS attribute = value selector of [tabindex='3']. This targets the login button by its tabindex attribute whose value is 3.
HTMLDoc.querySelector("[tabindex='3']").Click
Whole thing:
Option Explicit
Public Sub AttemptLogin()
Dim IE As New InternetExplorer
With IE
.Visible = True
.Navigate2 "https://willemendrees.nl/fn/login/"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
With .querySelector("#id_username")
.Focus
.Value = "amsterdam#willemendrees.nl"
End With
With .querySelector("#id_password")
.Focus
.Value = "***"
End With
.querySelector("[tabindex='3']").Click
End With
While .Busy Or .readyState < 4: DoEvents: Wend
Stop '<== Delete me later
.Quit
End With
End Sub
It seems there are two form elements on that html page and the login is the second. Identify, then submit the form.
HTMLDoc.all.id_username.Value = "amsterdam#willemendrees.nl"
HTMLDoc.all.id_password.Value = "*****"
HTMLDoc.getelementsbytagname("form")(1).submit
The index number of a collection of elements is zero-based.

VBA Auto Login Web page and fetch the data

I am trying to login Web page and fetch data however my login details are not getting update, i have tried all possibilities code from your forum, nothing is working for me
Below is my code, am getting attached error
Sub test()
Dim ie As Object
Dim objCollection As Object
Dim i As Integer
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "https://portal.expeditors.com/expo/login"
Do While ie.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
'Get all the elements with input tag name
Set objCollection = ie.document.getElementsByTagName("input")
i = 0
'Loop through all elements and find login form and fill it
While i < objCollection.Length
'Login name
If objCollection(i).Name = "username" Then
objCollection(i).Value = "bom-sumand"
End If
'Store login button in object
If objCollection(i).Type = "submit" Then
Set objElement = objCollection(i)
End If
i = i + 1
Wend
'Click login
objElement.Click
'Clean up
Set ie = Nothing
End Sub
I would use the available ids rather than looping to find the input boxes and sign in. These are much faster selector methods. You can add a .Focus. Also, swop InternetExplorer for InternetExplorerMeduim in some cases.
If problem continues check your internet settings in case site is blocked.
Open the URL via creating an IE instance direct.
Option Explicit
Public Sub Login()
Dim ie As New InternetExplorer 'InternetExplorerMedium
Const MAX_WAIT_SEC As Long = 5
Dim t As Date, ele As Object
With ie
.Visible = True
.navigate "https://portal.expeditors.com/expo/login"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
Do
DoEvents
On Error Resume Next
Set ele = .getElementById("j_username")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While ele Is Nothing
If ele Is Nothing Then Exit Sub
With ele
.Focus
.Value = "bob"
End With
With .getElementById("j_password")
.Focus
.Value = "penny"
End With
.getElementById("signInBtn").Click
End With
While .Busy Or .readyState < 4: DoEvents: Wend
Stop '<== Delete me later
.Quit
End With
End Sub
Macro trying and failing to open a second instance of IE try this.
Sub test()
Dim ie As Object
Dim redURL As String
Dim objCollection As Object
Dim i As Integer
redURL = "https://portal.expeditors.com/expo/login"
On Error Resume Next
Set ie = GetObject(, "InternetExplorer.Application")
If Err Then
Set ie = CreateObject("InternetExplorer.Application")
End If
On Error GoTo 0
ie.Visible = True
ie.Navigate redURL
Do While ie.Busy
Loop
End Sub

Excel VBA code to click web button

Need help how to create excel vba code for this
I'll be needing the codes so I can complete my macro.
Thanks in advance
First, you will need to create a reference to:
Microsoft Internet Controls
Microsoft HTML Object Library
In VBE, click Tools > References
Sub clickLink()
Dim ie As New InternetExplorer, Url$, doc As HTMLDocument
Url = "http://UrlToYourLink.com"
With ie
.navigate Url
Do While .Busy Or .readyState < READYSTATE_COMPLETE
DoEvents
Loop
doc = .document
.Visible = True
End With
Dim myBtn As Object
Set myBtn = doc.getElementsByClassName("button rounded")(0)
myBtn.Click
End Sub
The Internet control is used to browse the webpage and the HTML Objects are used to identify the username and password textboxes and submit the text using the control button.
Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Sub Login_2_Website()
Dim oHTML_Element As IHTMLElement
Dim sURL As String
On Error GoTo Err_Clear
sURL = "https://www.google.com/accounts/Login"
Set oBrowser = New InternetExplorer
oBrowser.Silent = True
oBrowser.timeout = 60
oBrowser.navigate sURL
oBrowser.Visible = True
Do
' Wait till the Browser is loaded
Loop Until oBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = oBrowser.Document
HTMLDoc.all.Email.Value = "sample#vbadud.com"
HTMLDoc.all.passwd.Value = "*****"
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
' oBrowser.Refresh ' Refresh If Needed
Err_Clear:
If Err <> 0 Then
Debug.Assert Err = 0
Err.Clear
Resume Next
End If
End Sub
The program requires references to the following:
1 Microsoft Internet Controls
2. Microsoft HTML Object Library
Microsoft internet controls are a great way to do this, but if you aren't allowed to add new references, here is another way to go about web scraping.
This methode ain't as 'clean' as Microsoft internet controls and HTML object but it gets the job done.
Sub GoogleSearch()
Dim ie As Object
Dim objSearchBnt As Object
Dim objCollection As Object
Dim i As Integer
'initialize counter
i = 0
'Create InternetExplorer Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
'navigate to the url
ie.navigate "Www.google.com"
'Statusbar shows in the buttom corner of excel
Application.StatusBar = "Loading, please wait..."
'Wait until page is ready
Do While ie.busy
Application.Wait DateAdd("s", 1, Now)
Loop
'Store all the elements with input tag
Set objCollection = ie.Document.getElementsByTagName("input")
'Go through all input elements
While i < objCollection.Length
'input search field
If objCollection(i).Name = "q" Then
objCollection(i).Value = "Hello World"
End If
'search button
If objCollection(i).Type = "submit" Then
Set objSearchBnt = objCollection(i)
End If
i = i + 1
Wend
objSearchBnt.Click
'Clean up
Set objSearchBnt = Nothing
Set objCollection = Nothing
Set ie = Nothing
'Give excel control over the status bar agian
Application.StatusBar = ""
End Sub