Navigate a Site Use getElementsbyTagName

kazshaz

New Member
Joined
Apr 20, 2015
Messages
9
I am trying to navigate through a website by using getElementsbyTagName. It appears after my first trial it does not work and there is an error.
Rich (BB code):
the_start: 
Set ie = CreateObject("InternetExplorer.Application") 
ie.Visible = True 
ie.Top = 0 
ie.Left = 0 
ie.Width = 800 
ie.Height = 600 
 
URL = Insurance Regulatory Authority 
ie.Navigate URL 
Do 
    DoEvents 
    If Err.Number <> 0 Then 
        ie.Quit 
        Set ie = Nothing 
Goto the_start: 
    End If 
     
Loop Until ie.ReadyState = 4 
Dim Doc As HTMLDocument 
Set Doc = ie.Document 
Dim Std As String 
 
Set AllLinks = Doc.getElementsByTagName("a") 
 
For Each hyperlink In AllLinks 
     
    If InStr(hyperlink.href, "/index.php/acts") > 0 Then 
        hyperlink.Click 
         
        Exit For 
    End If 
Next 
 'going on to the next website
Do 
    DoEvents 
    If Err.Number <> 0 Then 
        ie.Quit 
        Set ie = Nothing 
Goto the_start: 
    End If 
Loop Until ie.ReadyState = 4 
 ' it is fine up till this point
 
Set AllLinks = Doc.getElementsByTagName("a") 
For Each hyperlink In AllLinks 
    MsgBox hyperlink.innerText & " - " & hyperlink.href 
Next 
End Sub 
 ' it loops once for the search but then I get a debugging error
Could someone give me some advice on where I am going wrong. i've noticed T in innertext is capitalised as well if that makes a difference
 
Last edited:

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
A few questions:

What is the url? Is that:
Url = "http://www.ira.go.ke/"

What is the use of the:
Goto the_start:
statement?

That doesn't make sense to me because it takes you back to the beginning while the url stays the same.

When I step through your code with F8 it gives me a whole bunch of MsgBox. To be precise:99.

Step through your code with F8, then it wil work. When you just launch the code you get the error 70.
 
Upvote 0
Thanks for the response strooman
That is the URL , I tried to copy the link but that how it came up.
I am new to VBA, I've been using Alex Cantu's and MrExcelVbais fun videos to help me out. So I would assume its from there. I assume if the page is not loading correctly, it repeats the process

Yes, I've done that, I only seem to get "one" Msgbox though and of course the beautiful Run time error '70' . Permission denied.

Any ideas on how to get around it.
 
Upvote 0
Just create the full url right away:
Url = "http://www.ira.go.ke/index.php/acts"
so you can skip the part where you are looking for the right link.

Code:
Set AllLinks = Doc.getElementsByTagName("a") 
 
For Each hyperlink In AllLinks 
     
    If InStr(hyperlink.href, "/index.php/acts") > 0 Then 
        hyperlink.Click 
         
        Exit For 
    End If 
Next

Next, replace MsgBox by Debug.Print to print to the Immediate Window.
In the Visual Basic Editor go to View > Immediate window (or hit Ctrl+G) to show it.

Instead of using Internet Explorer you can also use "msxml2.xmlhttp" protocol which is faster.
A quick example:
Make sure you set a reference to the "Microsoft HTML Object Library". Tools > Reference.

Code:
Sub Search_Links()
'Note: Needs a reference to the "Microsoft HTML Object Library"
Dim strHTML As String
Dim objHTML As Object
Dim obj_Url As Object

    With CreateObject("msxml2.xmlhttp")
        .Open "GET", "http://www.ira.go.ke/index.php/acts", False
        .send
        strHTML = .responseText
    End With
    
    Set objHTML = New HTMLDocument
    objHTML.body.innerHTML = strHTML
    
    For Each obj_Url In objHTML.getElementsByTagName("a")
        Debug.Print obj_Url.innerText & " - " & obj_Url.href
    Next
    
End Sub
 
Upvote 0
Dude, you are awesome!

Thank you soo much! you don't understand how relieved I am. I've been sitting at the computer trying to figure that out for a good few days.

Is there a system where I can +1 rep you

For future reference, where do you learn stuff like this.
 
Upvote 0
Is there a system where I can +1 rep you

No need, glad I could help you. I think the like button in the right corner of the post is for that purpose.

For future reference, where do you learn stuff like this.

Just read a lot, Google, and read all kinds of Excel forums. There are a lot of clever men/women out there who will help you.
The "Locals window" in the Visual Basic Editor is always a great help for me.
 
Upvote 0

Forum statistics

Threads
1,223,101
Messages
6,170,116
Members
452,302
Latest member
TaMere

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top