locked
Spliting a row into 1 row per line in a table in MS Word - VBA RRS feed

  • Question

  • Is there a way to automatically split a row in a table that contains many lines into 1 row per line in MS Word using VBA?. (Currently each line is either blank or non blank).

    My goal is to have each line in its own row so that it would be easier later on to use VBA to select different items in the table when using loops, etc.

    The number of rows to split should be determined by how many lines there currently are in the row (some lines are blank).

    e.g. Currently, the four lines below are all contained within one row/cell (in a table), I would like to have it split into 4 rows.

    line#1

    <blank line> (line#2)

    line#3

    line#4

    Monday, June 2, 2014 12:35 AM

All replies

  • Without seeing the table, the answer is going to have to be no. If the table comprised a single column it might be possible to do something along the lines you suggest, but the difficulty comes with determining what is in the other columns and how they relate to the (unidentified) column that you wish to split.

    If you simply split the cells in one column leaving the others whole, then you are making any future programming task even more difficult. VBA doesn't like split cells.

    It would probably be simpler to address the table using VBA as it currently stands. It is relatively simple to process the various paragraphs (I hope you mean paragraphs and not merely wrapped lines of text) in a cell using VBA. e.g.

    Dim oTable As Table
    Dim oColumn As Column
    Dim oCell As Cell
    Dim oRng As Range
    Dim oLine As Range
    Dim i As Long
        Set oTable = ActiveDocument.Tables(1) 'the table to process
        Set oColumn = oTable.Columns(1) 'the column in the table
        Set oCell = oColumn.Cells(1) 'the first cell in the column
        Set oRng = oCell.Range
        oRng.End = oRng.End - 1 'remove the end of cell marker from the range
        If oRng.Paragraphs.Count > 1 Then 'is it a multi-paragraph cell?
            For i = 1 To oRng.Paragraphs.Count 'process each paragraph
                Set oLine = oRng.Paragraphs(i).Range
                oLine.End = oLine.End - 1 'remove the paragraph break from the range
                MsgBox "Line " & i & " contains:" & vbCr & _
                oLine.Text 'display the paragraph content.
            Next i
        End If


    Graham Mayor - Word MVP
    www.gmayor.com

    Monday, June 2, 2014 5:26 AM