Consulting

Results 1 to 4 of 4

Thread: Converting HTML into a Word Table

  1. #1
    VBAX Regular
    Joined
    Apr 2016
    Posts
    14
    Location

    Converting HTML into a Word Table

    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/2...ment-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

  2. #2
    VBAX Expert
    Joined
    Oct 2012
    Posts
    726
    Location
    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

  3. #3
    VBAX Regular
    Joined
    Apr 2016
    Posts
    14
    Location
    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

  4. #4
    VBAX Regular
    Joined
    Apr 2016
    Posts
    14
    Location
    Also it does multiple tables

    Added Bonus

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •