0

I was trying to pull a table from Webpage, so far i was successful pull a table from Webpage, unfortunately i have some links in each row of table, when i pulled the table from Webpage, i am getting output without link, just text, is there any way we can pull table from webpage using VBA including hyperlinks.

Here is my code:

Sub TableExample()
Dim IE As Object
Dim doc As Object
Dim strURL As String

strURL = "HERE I USED MY URL"
' replace with URL of your choice

Set IE = CreateObject("InternetExplorer.Application")
With IE
'.Visible = True

.Navigate strURL
Do Until .readyState = 4: DoEvents: Loop
Do While .Busy: DoEvents: Loop
Set doc = IE.Document
GetAllTables doc

.Quit
End With
End Sub

Sub GetAllTables(doc As Object)

' get all the tables from a webpage document, doc, and put them in a new worksheet

Dim ws As Worksheet
Dim rng As Range
Dim tbl As Object
Dim rw As Object
Dim cl As Object
Dim tabno As Long
Dim nextrow As Long
Dim I As Long

Set ws = Worksheets.Add

For Each tbl In doc.getElementsByTagName("TABLE")
tabno = tabno + 1
nextrow = nextrow + 1
Set rng = ws.Range("B" & nextrow)
rng.Offset(, -1) = "Table " & tabno
For Each rw In tbl.Rows
For Each cl In rw.Cells
rng.Value = cl.outerText
Set rng = rng.Offset(, 1)
I = I + 1
Next cl
nextrow = nextrow + 1
Set rng = rng.Offset(1, -I)
I = 0
Next rw
Next tbl

ws.Cells.ClearFormats

End Sub

1 Answer 1

1

When you execute "rng.Value = cl.outerText" you get only text. If you need to have all links and other html - please use innerHTML property.

Just replace "rng.Value = cl.outerText" to "rng.Value = cl.innerHTML". This will return entire html with links ;)

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your reply, i used as you mentioned, but it showing as "<A href="Reisen-Sea-Cloud-2/1145/exklusive-kreuzfahrten-sea-cloud-2-reise.html">1145</A>" It would be great if it shows as "1145" with excel hyperlink to that page.
That's not possible to have multiple links in one excel cell. But if you need to store only first link for each cell you can use your sript to extract text from web-page and after command "rng.Value = cl.outerText" add link to this cell using following steps: 1) get html using innerHTML property to some variable 2) get substring between quotes after href= (there can be single or double quotes) 3) add hyperlink ws.Hyperlinks.Add Anchor:=rng, Address:=YourVariableWithSubstring
I am sorry, if i explained wrongly, currently in one cell <A href="Reisen-Sea-Cloud-2/1145/exklusive-kreuzfahrten-sea-cloud-2-reise.html">114‌​5</A>, but i want that cell as "1145", but that has to be link to "Reisen-Sea-Cloud-2/1145/exklusive-kreuzfahrten-sea-cloud-2-reise.html"
sorry it was my first answers on this site so I press Enter instead of Shift+Enter to create new line. Please read my comment above.
I am sorry, i am not that much expert to write that type of code, i just googled the code. so i can understand your code, but not to write.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.