2

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.

Community
  • 1
  • 1
Syed Atif
  • 23
  • 1
  • 5
  • When error raises, press debug, type in immediate window `? TypeName(ie)`, and post the output. Check [this answer](https://stackoverflow.com/a/47084865/2165759) also. – omegastripes Nov 06 '17 at 09:33
  • After typing "? TypeName(ie)" into the immediate window I found the following message: IWebBrowser2 – Syed Atif Nov 06 '17 at 09:44
  • Thank you very much for your valuable reply. I also tried the following one it it worked exactly for what I wanted. ie.Document.forms("result").elements("rollno").Value = "12" – Syed Atif Nov 06 '17 at 11:27

1 Answers1

2

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
T. Nesset
  • 417
  • 3
  • 15
  • There is another problem it does not download required data after moving to the next page and but the cells left blank. The code I am using to fill excel cell with page data is mention below. ie.Document.getElementsByTagName("td")(7).innerText ie.Document.getElementsByTagName("td")(9).innerText ie.Document.getElementsByTagName("td")(11).innerText Please help to solve this problem. – Syed Atif Nov 07 '17 at 03:36
  • Well you never specified what data you want and in what cell you would like the data? – T. Nesset Nov 07 '17 at 06:56
  • "uhs.edu.pk/results/etr2015.php"; is the page where I enter Student Roll Number like Roll # 12. It brings the result to the next page and I need the same data in excel cells like a2, b2, c2, d2....... The result is mentioned Ahead::::::::::: :Sr. No: 12.. Roll No. 12... Name of the Candidate: ALISHA FATIMA.... Father's Name: SYED TARIQ BUKHARI.... Entrance Test Marks: 916 etc... – Syed Atif Nov 07 '17 at 11:29
  • Give the last code in my answer ago :) Should do the trick for you – T. Nesset Nov 07 '17 at 11:48
  • U r great, It really worked and solved my problem. Hats off to you...... – Syed Atif Nov 07 '17 at 12:48
  • Please also guide from where can I learn such coding. I also want to do it on expert level same like you – Syed Atif Nov 07 '17 at 12:51
  • 1
    Unfortunate there aren't many guides to VB or VBA. Alot sees this as a outdated programming language, but I'm fairly sure it will stick around for years yet. I'm far from expert in VBA, but I try to use it as much as possible to learn more. IE automation comes to be what i've done the most of so far. My best advise would be learn by doing... Stack overflow is a great place to find inspiration, but do not copy every code. then you will never understand how it truly works. Search inspiration and build your own code. – T. Nesset Nov 07 '17 at 13:55
  • Dear, I found another problem. Now on the same site "http://uhs.edu.pk/results/etr2017.php", there is human verification CAPTCHA. Is there any solution to read the image data in order to enter in the code textbox ? – Syed Atif Nov 08 '17 at 10:26
  • There are no captcha when i enter the site. I've seen multiple users automating captcha with VBA so it's indeed doable. – T. Nesset Nov 08 '17 at 11:17
  • Need Help to select combox value in webpage. Address is ahead "http://result.biselahore.com/" – Syed Atif Nov 14 '17 at 09:56
  • Our security system at work is blocking that site. Will take a look at home after work in a couple of hours. – T. Nesset Nov 14 '17 at 11:16
  • ie.Document.getElementsByTagName("option").Item(1).Selected = True It worked perfectly. The page contains two combo boxes. The aforementioned code is perfect for the first one. What about the second combo box ???..... – Syed Atif Nov 15 '17 at 06:36
  • Without having seen the source code for the page i would assume you also can fetch it with `ie.Document.getElementsByTagName("option")` – T. Nesset Nov 15 '17 at 14:26
  • will setup a Virtual Machine that dont have restricted web access later so i can visit the http page. – T. Nesset Nov 15 '17 at 14:28
  • Ill got it working. will add it to my answer. I didnt to the press button and roll etc. Assum you figured that out allready. – T. Nesset Nov 15 '17 at 19:00
  • I also struggled to solve the answer. Following are the two solutions. 1st one: ie.Document.getElementById("name").Item(3).Selected = True 2nd One: Set e = ie.Document.getElementById("name") Dim o For Each o In e.Options If o.Value = "2016" Then o.Selected = True Exit For End If Next – Syed Atif Nov 20 '17 at 05:38