Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleGenerate HTML versions of all of a Word document's tables
DescriptionThis example shows how to generate HTML versions of all of a Word document's tables in VBA.
KeywordsHTML, Word, Microsoft Word, table
CategoriesOffice, Strings
 
This example generates an HTML representation of simple Word tables. Function TableToHtml returns an HTML representation for a table. It loops through the table's rows and columns adding the appropriate HTML tags to the result string. This routine cannot handle complex tables that contain other tables, cells that span rows or columns, etc. It also doesn't try to provide an HTML table header row.
 
' Return an HTML representation of a simple table.
Private Function TableToHtml(ByVal tbl As Table) As String
Dim r As Integer
Dim c As Integer
Dim txt As String

    txt = "<TABLE>" & vbCrLf
    For r = 0 To tbl.Rows.Count - 1
        txt = txt & "  " & "<TR>" & vbCrLf
        For c = 0 To tbl.Columns.Count - 1
            txt = txt & "    " & "<TD>" & _
                TrimNonPrinting(tbl.Cell(r, c).Range.Text) _
                    & _
                "</TD>" & vbCrLf
        Next c
        txt = txt & "  " & "</TR>" & vbCrLf
    Next r
    txt = txt & "</TABLE>" & vbCrLf

    TableToHtml = txt
End Function
 
Function TrimNonPrinting removes non-visible characters from the front and back of a string. The TableToHtml function uses this to remove the non-printing character that ends a cell's range text.
 
' Remove leading and trailing non-printable characters from
' the string.
Private Function TrimNonPrinting(ByVal txt As String) As _
    String
Dim ch As String

    ' Remove leading characters.
    Do While Len(txt) > 0
        ch = Left$(txt, 1)
        If (ch >= " ") And (ch <= "~") Then Exit Do
        txt = Mid$(txt, 2)
    Loop

    ' Remove trailing characters.
    Do While Len(txt) > 0
        ch = Mid$(txt, Len(txt))
        If (ch >= " ") And (ch <= "~") Then Exit Do
        txt = Mid$(txt, 1, Len(txt) - 1)
    Loop

    TrimNonPrinting = txt
End Function
 
Function AllTablesToHtml loops through the active document's tables and calls TableToHtml to generate HTMl versions for each. Subroutine PrintAllTables calls AllTablesToHtml and displays the results in the Debug window.
 
' Return an HTML representation of all of the active
' document's tables.
Private Function AllTablesToHtml()
Const TABLE_SEP As String = "<HR>" & vbCrLf
Dim tbl As Table
Dim txt As String

    For Each tbl In ActiveDocument.Tables
        txt = txt & TABLE_SEP & TableToHtml(tbl)
    Next tbl

    If Len(txt) > 0 Then txt = Mid$(txt, Len(TABLE_SEP) + 1)
    AllTablesToHtml = txt
End Function

' Display HTML versions of all of the document's tables in
' the Debug window.
Private Sub PrintAllTables()
    Debug.Print AllTablesToHtml
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated