Fixing up InfoPath exported .mhts in Word


If you export a complex InfoPath view to web archive (.mht) file then edit it in Word, you may notice that Word takes a long time to paginate the document when you switch to print layout for the first time. This is happening because the .mht file has a lot of tall and nested tables spanning across multiple pages and it is hard for Word to layout those tables on pages.

So what can we do about it? Not much from InfoPath side, as most those tables are produced by InfoPath container controls such as repeating sections, lists, etc. However, from Word side, if you think about it, those tables do not serve as containers any more. And if they only have one column, they do not do what tables in Word suppose to do: layout things on pages. So we can just get rid of those (just the grid, we need keep the content of course). And that magic can be performed by the ConvertToText method of Table object, as shown below:


 

Sub UnNestTables()

    Dim tbP As Table

    For Each tbP In ActiveDocument.Tables

       UnNestTable tbP

    Next

End Sub


 

Sub UnNestTable(tbl As Table)

    Dim tblChild As Table

    For Each tblChild In tbl.Tables

        UnNestTable tblChild

    Next

    If tbl.Columns.Count = 1 Then

        tbl.ConvertToText wdSeparateByParagraphs, False

    End If

End Sub


 

After running the "UnNestTables" macro above in Word, all one-column tables will be removed. You should no longer experience the performance lag mentioned above. In addition, you'd find the document looks much neater and easier to work with.


Skip to main content