none
HTML table to Word Table RRS feed

  • Question

  • Hi,

    I have a word document where I am writing some HTML mark up automatically from another system into Word document's rich text content control. However, the Rich text content control shows the HTML mark up  and does not translate the Mark up automatically to a word table. 

    How can I achieve this using minimum code?

    Please advise,

    Thank you


    techie

    Friday, December 2, 2016 11:46 PM

All replies

  • You could save the html code as an html file in its own right, then open that with Word and use either copy/paste or the FormattedText method to insert the content into the content control.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, December 3, 2016 2:55 AM
  • Hi Cooltechie1234,

    if you try to put html code in VBA and try to save it as an HTML then when you try to open the word file then it will display you as text not like table.

    if you wan to decode the HTML in VBA and try to create table then you have to find Tags in your HTML code and according to that try to execute similar commands in VBA and try to create Table. but I think it will be complex and time consuming.

    if you manually copy paste the table from HTML page to Word then it will display as table. but maybe not suitable approach for you.

    if you have an option to try third party libraries then you can try for spire for doc.

    I try to visit the link and its looks like it can convert the HTML content to word document.

    please refer the link mentioned below.

    How to Convert HTML to Word

    code is also simple:

    using System;
    using Spire.Doc;
    using Spire.Doc.Documents;
    
    namespace Html2Doc
    {
        class Program
        {
            static void Main(string[] args)
            {
                Document document = new Document();
                document.LoadFromFile(@"D:\test.html",FileFormat.Html,XHTMLValidationType.None);
                document.SaveToFile("test.doc", FileFormat.Doc);
            }
        }
    }

    if VBA is only option for you then try to decode the HTML Sting  and create the table again in VBA code.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 5, 2016 1:57 AM
    Moderator
  • Hi Deepak,

    In my scenario, the HTML conversion into word table must happen right after the HTML is copied. So, it would be ideal if this can be achieved through decoding HTML using VBA. 

    I am not a VBA Pro, hence I need some help in achieving this.  Please note that I am copying HTML code into Content control. 

    Please advise.


    techie

    Tuesday, December 6, 2016 9:23 PM
  • To do that without saving the HTML code to file, you'd have to paste the raw HTML code into the content control then use your own HTML parsing code to re-format it; Word doesn't have a built-in HTML parser for content pasted/typed directly into a document and neither does VBA have an HTML parser.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, December 6, 2016 9:32 PM
  • Hi Cooltechie1234,

    you had mentioned that,"I am not a VBA Pro, hence I need some help in achieving this.  Please note that I am copying HTML code into Content control. "

    you have to understand the code and you have to make modifications to make it work for you.

    here are some code blocks and links that I found and you can try to look in to that.

    1st approach:-

    1) Remove "<tr>" from the table definition;

    2) replace "</tr>" with ANSI 13 ("vbCr") and drop the last of these tags;

    3) Remove "<td>" from the table definition;

    4) Replace "</td>" with a field delimiter of your choice (something that's not in the field content) and drop the last of these tags.

    The result will be a string that you write to the Range where you want the table in the document, then convert it to a table:

    rng.Text = stringTableDef
    Dim tbl as Word.Table
    Set tbl = rng.ConvertToTable('Params here)

    Reference:

    Convert HTML response to WORD

    2nd Approach:-

    Sub PrintHTML_Table(ByVal StrTable as String)
        Dim TA()
    
        Dim Table_String as String
        Table_String = " " & StrTable & " "
    
        TA = SplitTo2DArray(Table_String, "</tr>", "</td>")
    
        For i = LBound(TA, 1) To UBound(TA, 1)
            For j = LBound(TA, 2) To UBound(TA, 2)
                ActiveSheet.Cells(i + 1, j + 1) = Trim(Replace(Replace(TA(i, j), "<td>", ""), "<tr>", ""))
            Next j
        Next i
    
        End Sub
    
    
    
    
        Public Function SplitTo2DArray(ByRef StringToSplit As String, ByRef RowSep As String, ByRef ColSep As String) As String()
    
            Dim Rows                    As Variant
            Dim rowNb                   As Long
            Dim Columns()               As Variant
            Dim i                       As Long
            Dim maxlineNb               As Long
            Dim lineNb                  As Long
            Dim asCells()               As String
            Dim j                       As Long
    
            ' Split up the table value by rows, get the number of rows, and dim a new array of Variants.
            Rows = Split(StringToSplit, RowSep)
            rowNb = UBound(Rows)
            ReDim Columns(0 To rowNb)
    
            ' Iterate through each row, and split it into columns. Find the maximum number of columns.
            maxlineNb = 0
            For i = 0 To rowNb
                Columns(i) = Split(Rows(i), ColSep)
                lineNb = UBound(Columns(i))
                If lineNb > maxlineNb Then
                    maxlineNb = lineNb
                End If
            Next i
    
            ' Create a 2D string array to contain the data in <Columns>.
            ReDim asCells(0 To maxlineNb, 0 To rowNb)
    
            ' Copy all the data from Columns() to asCells().
            For i = 0 To rowNb
                For j = 0 To UBound(Columns(i))
                    asCells(j, i) = Columns(i)(j)
                Next j
            Next i
    
            SplitTo2DArray = asCells()
    
        End Function

    Reference:

    Parsing HTML to recreate tables in a Word Document using VBA

    Disclaimer: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 7, 2016 4:45 AM
    Moderator
  • Parsing the html code for a table takes far more than that, though. One has to consider not only the number of columns & rows but also the cell widths, heights, borders, content and the formatting of that content. The simplest way of handling whatever the HTML code might contain would be to save it as an HTML file and insert that file's content into the content control, thus:

    Sub UpdateCCtrlWithTableHTML(StrHTML As String, DocTgt As Document, iCCtrl As Long)
    Dim DocHTML As Document, StrNameHTML As String
    StrNameHTML = "C:\Users\" & Environ("UserName") & "\Documents\Temp.htm"
    Set DocHTML = Documents.Add
    With DocHTML
      .Range.Text = StrHTML
      .SaveAs2 StrNameHTML, wdFormatText, , , False
      .Close False
    End With
    Set DocHTML = Documents.Open(StrNameHTML, , True, False)
    With DocHTML
      DocTgt.ContentControls(iCCtrl).Range.FormattedText = .Range.FormattedText
      .Close False
    End With
    Kill StrNameHTML
    End Sub

    Simply pass the: (a) HTML string; (b) destination document; and (c) content control index, to the above macro and the table it represents will be inserted into the relevant content control. Minor modifications could be made to allow the content control's title or tag instead of its index #.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, December 8, 2016 3:19 AM
  • Hi Deepak,

    Thank you very much for your response. Will it be easier if we try to parse an XML where each element becomes a row in word and attributes can be translated to columns?

    Please advise,

    Thank you,


    techie

    Thursday, December 8, 2016 7:02 PM
  • Hi Cooltechie1234,

    you had mentioned that you have HTML code and wan to create table in word.

    but from where you will get the XML code.

    what ever you do you need to follow the approach like mentioned above. you need to remove the tags and fill the remaining text in table cell.

    also as other community member already mentioned that it will not take care of any of the formatting of HTML Table.

    and you can see the limitation here of above mentioned code.

    so here 2 ways are available for you.

    (1) you can try to use spire.doc third party library to convert from HTML to Word. I already provided a link to you in my first post. 

    (2) Follow the approach suggested by macropod to save as the HTML file as word document.

    these are 2 possible ways to fulfill this requirement properly.

    otherwise it will going to be tough for you and also it will not convert the table properly.

    so try to choose the best suitable approach for you.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 9, 2016 4:23 AM
    Moderator