PDA

View Full Version : [SOLVED:] Converting HTML into a Word Table



sand60
06-01-2016, 09:50 AM
Hi all, :)

I am trying to come up with a way to convert html to a word table.

Sometimes I get pasted text that has html tables within and i need to find a way to see if i can extract that data into a table?

That is put it into a table format.

I found this excellent thread - but unfortunately I couldn't get it to work.

http://stackoverflow.com/questions/29483422/parsing-html-to-recreate-tables-in-a-word-document-using-vba

I have a sketch of whats involved:



1 Header - the th cells - need to be identified
Count how many <tr> there are - each <tr> is a row
Insert and space out the code





<th>1</th><th>Texas </th> <th>519</th><th>1</th></tr></thead><tbody>
<tr><td>2</td> <td>Illinois </td>
<td>502</td> <td>2</td></tr>
<tr><td>3</td><td>Stanford</td><td>486</td><td>5</td></tr>
<tr><td>4</td><td>Georgia</td><td>463</td><td>3</td></tr>
<tr><td>5</td><td>Oklahoma State</td><td>416</td><td>6</td></tr>
</tbody></table>



I tried recording a macro - that was no use.
I inserted tabs after the </td> to space it out, then convert, still no luck.


How can I use vba to convert this html table code to a Word Document table?

If any one has any tips - I would be grateful.
Also - this is a challenging task as I have been trying to get this to work for a while, my newbie skills really letting me down

thank you for your time

sand

jonh
06-10-2016, 06:59 AM
You don't need to convert/parse anything. Let Word do it for you.


Open the html file as a Word document and copy the table data.

Const htm = "C:\samples\sample.htm"


Sub GetTable()
Dim wa As New Word.Application
wa.Visible = 1
wt wa.Documents.Open(htm, Format:=wdOpenFormatWebPages)
End Sub


Sub wt(wd As Word.Document)
Dim wt As Word.Table
Dim rc, cc, r, c
For Each wt In wd.Tables
With wt
rc = .Rows.Count
cc = .Columns.Count
For r = 1 To rc
For c = 1 To cc
Debug.Print Replace(.Cell(r, c).Range.Text, vbCr & Chr(7), ""),
Next
Debug.Print
Next
Debug.Print
End With
Next
End Sub

sand60
06-10-2016, 08:04 AM
Hi Jonh,

thank you my friend, this gives me a really good starting point. :)

I could not get my version to work, and could not find anything else.

I wanted to be able to see specific information lost in the <tds> and

This made a nice table

Thank you again for your help

Sand

sand60
06-10-2016, 08:08 AM
Also it does multiple tables

Added Bonus
:content: