vbams-wordoffice-interopoffice-2010

duplicate a complex (double) table row in ms word vba


in Microsoft Word 2010, say I have a template with a table, which contains some ready column headers, and the first row. this row is complex - basically it spans two real rows. it also has uneven column widths in the first and the second "real" rows. e.g. enter image description here i am trying to populate this table with a dynamic number of rows, that i need to be like the first one. e.g. enter image description here the methods that i've seen this far that add rows to a table all make the new row look like the last row (or the first - depending on the way you do it) - but i want the same functionality to be extended to two rows, i.e. so that the new TWO rows would look like the last TWO rows. the only way i found is to use clipboard. i want to avoid clipboard. maybe anyone can think of another way?

the clipboard code that works:

Sub example()

    Set myrows = ActiveDocument.Range( _
        ActiveDocument.Tables.Item(1).Rows.Item(1).Range.Start, _
        ActiveDocument.Tables.Item(1).Rows.Item(2).Range.End _
    )

    myrows.Select

    ' this works
    myrows.Copy
    myrows.Paste

    ' this adds two rows that look like the last row in the selection
    ' this is not what i need
    'Selection.InsertRowsBelow

End Sub

thanks


Solution

  • I'm sorry, it's an example of forgetting what you learned like 10 years ago. The particular question I asked seems to be easily answered.

    Sub example()
    
        Set myrows = ActiveDocument.Range( _
            ActiveDocument.Tables.Item(1).Rows.Item(1).Range.Start, _
            ActiveDocument.Tables.Item(1).Rows.Item(2).Range.End _
        )
    
        ' this works
        Set rowsbelow = ActiveDocument.Range(myrows.End, myrows.End)
        rowsbelow.FormattedText = myrows.FormattedText
    
    End Sub
    

    if moderators deem it necessary, i may delete the post entirely