none
c# interop.word table row after row RRS feed

  • Question

  • hi,

    i am new to this framework.

    the following i have: lease read the comments inside code to understand it.

    i have table with 3 rows and 4 columns.

    i start inserting data from 3rd row. and i do this in loop.

    now when i try to add a new row right after the row i added (infact i have to merge all 4 columns in this row).

    it is actually added at the end of the table.

    please help me out of this problem.

    //this table has 1 row and 4 columns //the first two rows need to be skipped which works fine for the moment int row = 3; object bookmarkName = "Bookmark1"; Word.Bookmark bookmark = document.Bookmarks.get_Item(ref bookmarkName); Word.Table table = bookmark.Range.Tables[1]; //here i am able to add the row and in each column i can put some value foreach (Object ostr in listObject) {

    table.Rows.Add(); Word.Cell cell1 = table.Cell(row, 1); cell1.Range.Text = ostr.pop1; Word.Cell cell2 = table.Cell(row, 2); cell2.Range.Text = ostr.pop2; Word.Cell cell3 = table.Cell(row, 3); cell3.Range.Text = ostr.pop3; Word.Cell cell4 = table.Cell(row, 4); cell4.Range.Text = ostr.pop4; } //now the problem begins here //when i add the next row right after the row i added: the row is actually added at the end of the table and not after //the row i just added. //NORMALLY I HAVE TO MERGE ALL THE 4 COLUMNS IN THIS NEXT ROW. table.Rows.Add(); Word.Cell cellDesc = table.Cell((row + 1), 1); cellDesc.Range.Text = "new text in the next row";


    Monday, August 13, 2012 8:08 AM

Answers

  • You are able to supply a parameter with:

    table.Rows.Add();

    You can tell it which Row to add after. For your task, you need to know how many rows you've inserted. You don't say exactly what type listObject is; but assuming it's a List<T>:

    table.Rows.Add(listObject.Count + 3);

    Should do what you want.

    Monday, August 13, 2012 9:09 AM
  • Then count how many iterations are performed of the loop where you add rows, and then add three to that count.
    Monday, August 13, 2012 10:05 AM

All replies

  • You are able to supply a parameter with:

    table.Rows.Add();

    You can tell it which Row to add after. For your task, you need to know how many rows you've inserted. You don't say exactly what type listObject is; but assuming it's a List<T>:

    table.Rows.Add(listObject.Count + 3);

    Should do what you want.

    Monday, August 13, 2012 9:09 AM
  • the following code gives me bad parameter.

    table.Rows.Add(listObject.Count + 3);

    listObject is a Class of type Person with properties...

    please look forward



    Monday, August 13, 2012 9:37 AM
  • Then count how many iterations are performed of the loop where you add rows, and then add three to that count.
    Monday, August 13, 2012 10:05 AM