1

Problem I'm having is that table.Columns.add(ref Object BeforeColumn) requires a reference to another column in the table. However, when I try to access the last column in the table to pass as a reference using table.Columns.Add(table.Columns[table.Columns.Count])

I get the error:

"Cannot access individual columns in this collection because the table has mixed cell widths."

As my current work around, I catch the error, and call table.Columns.DistributeWidth() to make sure the columns are uniform and run the rest of the code. However, I lose the formatting of my cell widths this way, which is unfortunate.

Is there any way I can workaround this without losing the cell width?

(I realize one way is to store every cell's width before running this process, and then re-applying the widths afterward, but this seems like a very costly solution to something that should be simpler)

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Shark
  • 2,322
  • 5
  • 31
  • 44
  • This seems to be related to: http://stackoverflow.com/questions/5399061/how-to-access-columns-in-a-table-that-have-different-cell-widths-from-ms-word/8527573#8527573 – Fionnuala Dec 16 '11 at 00:05
  • I actually "answered" the question before I posed this question. It's a slightly different problem. – Shark Dec 16 '11 at 00:41

1 Answers1

1

I've found one way to do it. Here's how I approached it.

*Caution, I'm assuming that the table is uniform. i.e. The number of columns is the same across all the rows. (Note, the API has a Table.uniform function, but the description is not complete. In the API it says "True if all the rows in a table have the same number of columns." However, it also checks if the columns have uniform width).

Instead of using table.Columns.Add(table.Columns[table.Columns.Count]) to add a column before the last below, I select a cell in the table and used the insert command:

//assuming table is the name of the table you want to add columns to
table.Cell(1, table.Columns.Count).Select();
word.Selection selection = table.Application.ActiveWindow.Selection;
selection.InsertColumns();

This might actually be a better way to add columns, as the api gives you way more options on how to insert (i.e. use InsertColumnsRight to insert to the right of the column). The Columns.Add(...) function by default inserts to the left of the select

Shark
  • 2,322
  • 5
  • 31
  • 44