0

I'm working on a WordAutomation project and need to handle this table. I'm using visual studio 2010 and Word 2010.

I got this table:

+----------------------------------------------+
|                    1,1                       |
|----------------------------------------------|
|            |       2,2       |      2,3      |
|     2,1    |-----------------|---------------|
|            |       3,2       |      3,3      |
|----------------------------------------------|
|                    4,1                       |
+----------------------------------------------+

I want to add a row to the merged cell. I'm using this function:

table.Rows.Add(table.Rows[2]);

The function works fine when the cell isn't vertically merged. However it throws this error when executing on a vertically merged cell:

"Cannot access individual rows in this collection because the table has vertically merged cells."

I want this output:

+----------------------------------------------+
|                    1,1                       |
|----------------------------------------------|
|            |       2,2       |      2,3      |
|     2,1    |-----------------|---------------|
|            |       3,2       |      3,3      |
|            |-----------------|---------------|
|            |       4,2       |      4,3      |
|----------------------------------------------|
|                    5,1                       |
+----------------------------------------------+

Any workarounds would be appreciated!

4 Answers 4

0

Working on a similar Word Automation project using the Interop API, I ran into the same problem. Unfortunately, you cannot insert a row into the table when there are merged rows as you can't reference the row.

I assume you a working with a word document that you have created in advance which you are using as a template.

The only work around I could think of is to split all of the vertically merged cells back to normal. In your situation you'll now have 2,1 -> 4,1 as valid cells and you can call

table.Rows.Add(table.Rows[2]);

You can then merge the cells after you've added the row using;

document.Tables[1].Cell(2,1).Merge(document.Tables[1].Cells(4,1));

The code below will add into the table an unknown number or rows before Row 4 and then merge the cells on the left up to your original row 4.

int addedRows = 0;

foreach (string text_for_cell in listOfString) 
{
   // add a new row and set the contents of the cell 
   var new_row = document.Rows.Add(table[1].Rows[4 + addedRows]);
   new_row.Cell[2].Range.FormattedText.Text = text_for_cell;

   addedRows++;

}

document.Tables[1].Cells(2,1).Merge(document.Tables[1].Cells(3 + addedRows, 1);

The downside of this is that you're referencing row and column indexes. So any changes to your original template will need to be reflected in the code too.

2
  • The table is added to the document using code. So I'm already aware of these merging features. My tables are added from a factory and afterwards added information into the cells and adding new rows. So what I really wanted was a way to add rows with a vertically merged cell without merging them afterwards. So I don't have to do any table formatting after it comes out of my factory.
    – n.Stenvang
    Mar 11, 2015 at 11:40
  • One way would be to identify merged cells, split them, perform your operations and then merge them again. But I'm not sure that this is possible either. stackoverflow.com/questions/4269545/… Mar 11, 2015 at 23:24
0

I suggest you take a look at 3rd party toolkits. This will save you tons of time (and money) at the end. I know Docentric so I will tell how it could solve your problem.

First, you have to decide if you want a fixed document (e.g. you will create its entire DOM and contents programmatically) or a flow document (e.g. you will create a template with layout, formating and placeholders for data and then merge the data into the template at runtime).

Both of these options could be applied in your case. Template documents are much easier and faster to prepare. So if you can describe rules in Docentric enabled template, you are good to go. Because you can use conditional content using logical statements, you could probably solve your problem with this approach.

But if not, you can still create a document from scratch, using Docentric's DOM classes.

In both scenarios, you can choose .docx, .pdf or .xps output. Since report generation is based on OpenXML, no Word installation is necessary and it can be used on servers very effectively.

Here is an example how to create a report from a template to give you a clue.

1
  • That sounds like a nice toolkit but it's not really what I'm looking for.
    – n.Stenvang
    Mar 24, 2015 at 9:21
0

I know that it is an old post but I had a pretty similar problem adding rows to a table like this:

+----------------------------------------------+
|            |       1,2       |      1,3      |
|     1,1    |-----------------|---------------|
|            |       2,2       |      2,3      |
|----------------------------------------------|
|     3,1    |       3,2       |      3,3      |
+----------------------------------------------+

The problem is that you can't access table rows directly, it throws the error you mentioned. This code does not work:

table.Rows[2]

But you can still add rows and access cells, for example in my case to add a new row (4) and fill the cells I used:

int count = table.Range.Cells.Count;
table.Rows.Add();
table.Range.Cells[count + 1].Range.Text = "4,1 value";
table.Range.Cells[count + 2].Range.Text = "4,2 value";
table.Range.Cells[count + 3].Range.Text = "4,3 value";

So in your example a solution would be to add a new row with a reference to the cell 3.3. I've not tested this code but it should be the way to go:

table.Rows.Add(table.Range.Cells[6]);
table.Range.Cells[7].Range.Text = "4,2 value";
table.Range.Cells[8].Range.Text = "4,3 value";
0

You have to get the range of the table, then get a collection of cells out this range and finally you go cell by cell using item.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.