Adding new rows to a table in word document

Dec 23, 2013 at 10:12 AM
I have a word document with two tables defined in it,

I wonder how to add new rows/text data to the table?
May 22, 2014 at 1:05 PM
I have the same problem and if you could not get the answer from Dec 2013, I'm gonna die here :S
May 23, 2014 at 10:07 AM
Edited May 23, 2014 at 10:07 AM
Here's an example that I just created; my test document contains two tables (with two rows and two columns)
Hope this helps:
    foreach (Table t in document.Tables)
    {
        if (t.TableCaption.Equals("MyTable"))
        {
            t.Rows[0].Cells[0].Paragraphs[0].Append("First row, first cell");
            t.Rows[0].Cells[1].Paragraphs[0].Append("First row, second cell");
            t.Rows[1].Cells[0].Paragraphs[0].Append("Second row, first cell");
            t.Rows[1].Cells[1].Paragraphs[0].Append("Second row, second cell");
            //Create a new row at the end of the table
            Row r = t.InsertRow(t.RowCount);
            r.Cells[0].Paragraphs[0].Append("New row, first cell");
            r.Cells[1].Paragraphs[0].Append("New row, second cell");
        }
        
        if (t.TableCaption.Equals("MySecondTable"))
        {
            t.Rows[0].Cells[0].Paragraphs[0].Append("Second table. First row, first cell");
            t.Rows[0].Cells[1].Paragraphs[0].Append("Second table. First row, second cell");
            t.Rows[1].Cells[0].Paragraphs[0].Append("Second table. Second row, first cell");
            t.Rows[1].Cells[1].Paragraphs[0].Append("Second table. Second row, second cell");
            //Create a new row and use it as the first row in the table
            Row r = t.InsertRow(0);
            r.Cells[0].Paragraphs[0].Append("Second table. New row, first cell");
            r.Cells[1].Paragraphs[0].Append("Second table. New row, second cell");
        }
    }

    //It is also possible to access the table by index
    Row rx = document.Tables[0].InsertRow(document.Tables[0].RowCount);
    rx.Cells[0].Paragraphs[0].Append("Newest row, first cell");
    rx.Cells[1].Paragraphs[0].Append("Newest row, second cell");
Note: I used the table's Alternate Title (TableCaption) to distinguish between the two tables, this is only available in Word 2010 (documents) though.