Search code examples
c#ms-wordoffice-interop

Adding tables in word document in loop


I want to add multiple tables in word document programmatically. I have tried following code in order to add table (in sample code below I have not used loop though)

        Microsoft.Office.Interop.Word.Table imageTable1 = wordDoc.Tables.Add(initialRange, 1, 2, ref oMissing, ref oMissing);
        imageTable1.Rows.SetHeight(40, WdRowHeightRule.wdRowHeightExactly);
        imageTable1.AllowAutoFit = true;

        var text = "ABC";

        // Add feature name in bold in table
        if (!string.IsNullOrEmpty(text))
        {
            Cell cell1 = imageTable1.Cell(1, 1);
            cell1.Range.Bold = 1;
            cell1.Range.Underline = WdUnderline.wdUnderlineSingle;
            cell1.Range.Font.Size = 18;
            cell1.Range.Font.AllCaps = 1;
            cell1.Range.Font.Name = "Times New Roman";
            cell1.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
            cell1.Range.Text = "ABC";
        }
        initialRange.Collapse(WdCollapseDirection.wdCollapseEnd);
        initialRange.InsertParagraphAfter();
        initialRange.Collapse(WdCollapseDirection.wdCollapseEnd);


        Microsoft.Office.Interop.Word.Table imageTable2 = wordDoc.Tables.Add(initialRange, 1, 2, ref oMissing, ref oMissing);
        imageTable2.Rows.SetHeight(40, WdRowHeightRule.wdRowHeightExactly);
        imageTable2.AllowAutoFit = true;

        text = "DEF"
        // Add feature name in bold in table
        if (!string.IsNullOrEmpty(text))
        {
            Cell cell1 = imageTable2.Cell(1, 1);
            //cell1.Range.InsertAfter(feature.Name + Environment.NewLine);
            cell1.Range.Bold = 1;
            cell1.Range.Underline = WdUnderline.wdUnderlineSingle;
            cell1.Range.Font.Size = 18;
            cell1.Range.Font.AllCaps = 1;
            cell1.Range.Font.Name = "Times New Roman";
            cell1.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
            cell1.Range.Text = "DEF";
        }

In above code, initialRange variable is Selection range in my document. With above code, I get overlapped tables and only last table is visible when document is opened. Code creates all the tables properly but all tables get placed at same location and hence table which is created last is only visible. I'm not able to figure out what change is needed in above code in order to show the tables one after another.

Also, I would like to add some text lines between tables. How I can insert the text so that in my document I have table followed by its relevant text.

Thanks


Solution

  • The problem is not that the tables are overlapping. What's happening with the code in the question is that the subsequent table is being inserted into a cell in the previous table. The reason is because initialRange does not contain the entire table that's added at the Range - initialRange is in the first cell of the table.

    The trick is to put the Range object at the end of the table, something like this:

    initialRange = imageTable1.Range;
    initialRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
    initialRange.InsertParagraphAfter();
    initialRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd);