How to access columns in a table with different cell widths from MS Word

I am trying to get cells from 1st column in a table. Getting an exception in " Foreach(Cells c in rng.Tables[1].Columns[1].Cells) " because the table contains columns with a mixed cell width.

for example: in the first row there are 4 cells, and in the second row there are only 2 cells (2 cells are combined together)

Error message: It is not possible to access individual columns in this collection because the table has a mixed cell width. "

 Document oDoc = open word document foreach (Paragraph p in oDoc.Paragraphs) { Range rng = p.Range; /* */ foreach (Cell c in rng.Tables[1].Columns[1].Cells) { //.... } } 
+4
source share
1 answer

Instead of using the foreach loop in the second loop, you can instead use the for loop to iterate over all the cells:

  for (int r = 1; r <= rng.Tables[1].Row.Count; r++) { for (int c = 1; c <= rng.Tables[1].Columns.Count; c++) { try { Cell cell = table.Cell(r, c); //Do what you want here with the cell } catch (Exception e) { if (e.Message.Contains("The requested member of the collection does not exist.")) { //Most likely a part of a merged cell, so skip over. } else throw; } } } 
+4
source

Source: https://habr.com/ru/post/1344851/


All Articles