Reputation: 433
I need to programmatically add and remove rows from a Word 2010 table. Unfortunately the header of the table contains merged cells, both horizontally and vertically merged. That causes an error when using the Row.Add and Row.Delete methods. I have tested and discovered that I can programmically remove the merged cells (Cell.Split) and then do the .Add and .Delete methods and then restore the merged cells. The problem I'm having is identifying which cells that are merged.
-------------------------
| 1,1 | 1,2 | 1,3 | 1,4 |
-------------------------
| 2,1 | 2,2 | 2,3 | 2,4 |
-------------------------
If cells 1,1 and 2,1 are vertically merged then accessing Table.Cell(2,1) throws an error. Which is good. But, if cells 1,1 and 1,2 are horizontally merged, accessing 1,2 does not throw an error but accessing 1,4 does. Which means that I can't determine which cells are horizontally merged.
What I'm trying to do is let the design people change the look and feel of a Word document but the data in the table is populated by an SQL query that people fill in using a web app.
My question, is there a way to determine the layout of a table so that I can remove the merged cells and recreate them after adding and removing rows?
Thanks, Aaron
Upvotes: 2
Views: 5058
Reputation: 433
Well I ended up describing the layout of the table in the Table Properties / Alt Text / Decription area. And then reading that in the code to split the cells, delete rows and then remerged the cells. Crude but it works.
Upvotes: 0
Reputation: 4879
When dealing with tables that might contain merged cells, you always have to start with CELL(1,1) and step through the cells using CELL.NEXT (Next is a property of the CELL object)
If you do that, you can interrogate what row and column the "current" cell is. You +can not+ reference cells by row col directly in tables with merged cells or you'll get the error you mention.
Then just continue through till NEXT returns nothing.
Upvotes: 3
Reputation: 24132
You will have to put off merging until you have dealt with all of the records.
Word doesn't work well with tables.
I suggest you create an Excel Workbook, and link the Worksheet from your Excel Workbook to your Word Document. Excel deals best with tables.
Upvotes: 0