Articles in this section
Category / Section

Is it possible to add/remove column from the table ?

1 min read

Yes, it is possible to add or delete a column from a table. There is no property to directly add or remove columns from the table. By Microsoft Word design, rows in a table in a document are completely independent. This means each row can have any number of cells of any width. So, if you need to add a column to an MS Word table, you have to add a cell for each row in the table. Therefore, we need to add a cell for each row by looping. Please refer to the below code snippet to achieve this


C#

//To add/remove a column into the table

for (int i = 0 ; i < table.Rows.Count ; i++)

{

WTableCell cell = new WTableCell(document);

cell.AddParagraph().AppendText(i.ToString());

table.Rows[i].Cells.Add(cell);

//or Use Addcell method to add new cell

//table.Rows[i].AddCell().AddParagraph().AppendText(i.ToString());

//Insert colum at specific column

//table.Rows[i].Cells.Insert(table.Rows[i].Cells.Count-1, cell);

//Set width of the cell

table.Rows[i].Cells[2].Width = 50;

table.Rows[i].Cells[3].Width = 50;

//To remove cell from the row

table.Rows[i].Cells.RemoveAt(0);

}

VB

For i As Integer = 0 To table.Rows.Count - 1

''To add/remove a column into the table

Dim cell As New WTableCell(document)

cell.AddParagraph().AppendText(i.ToString())

table.Rows(i).Cells.Add(cell)

''or Use AddCell method to add a new cell

''table.Rows[i].AddCell().AddParagraph().AppendText(i.ToString());

''Insert column at a specific column

''table.Rows[i].Cells.Insert(table.Rows[i].Cells.Count-1, cell);

''Set the width of the cell

table.Rows(i).Cells(2).Width = 50

table.Rows(i).Cells(3).Width = 50

''To remove a cell from the row

table.Rows(i).Cells.RemoveAt(0)

Next

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied