Adding tables in word document in loop

北战南征 提交于 2020-01-30 12:27:05

问题


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


回答1:


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);


来源:https://stackoverflow.com/questions/55050032/adding-tables-in-word-document-in-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!