none
I can't believe how hard this is - Insert line of text before a table in MS Word RRS feed

  • Question

  • I am new to VBA, but a long-time programmer.  

    I am converting all tables to text - no problem.  But before I do, I want to remember where the table began and ended, so before each table I want to insert "~>Table n~" and after each table insert "~<Table n"~, where n is the table number.   I can insert the tag in a paragraph after the table, no problem, but I cannot find a way to insert a line of paragraph text before a table without embedding it in the table itself.  

    Any help would be appreciated.  

    Thanks.

    Konacq

    Friday, May 3, 2019 3:26 PM

All replies

  • The secret is to split the table at its start and write in the space e.g.

    Sub Macro1()
    Dim oTable As Table
    Dim oRng As Range
    Dim i As Integer
        For i = ActiveDocument.Tables.Count To 1 Step -1
            'start with the last table so as not to screw up the count'
            Set oTable = ActiveDocument.Tables(i)
            Set oRng = oTable.Range
            oRng.Collapse 1
            'select the start of the table'
            oRng.Select
            'split the table to create a paragraph above'
            Selection.SplitTable
            'add the text before the table'
            oRng.Text = "Table " & ActiveDocument.Tables.Count
            'convert it to text'
            oTable.ConvertToText
            'and repeat'
        Next i
    lbl_Exit:
        Set oRng = Nothing
        Set oTable = Nothing
        Exit Sub
    End Sub
    



    Graham Mayor - Word MVP
    www.gmayor.com

    Saturday, May 4, 2019 4:26 AM