tags:

views:

324

answers:

2

I am creating and populating a Word 2007 table in C#. When I view the result in Word, each cell has extra vertical space appended after the text. In Word this can be adjusted through the "page layout"/Paragraph/Spacing, where the initial value is 10pt.

---------------------------------------------------
| Text...     | Text....  | More text...          |
|             |           |                       | <- Extra spacing
---------------------------------------------------
|             |           |                       |

How can this be changed using VSTO?

I have tried to record a macro, hoping for some answers in the VB code - it didn't seem to respond to the changing of the spacing value.

I haven't been able to find anything related in the VSTO documentation on MSDN.

Edit: Using a Word template, I can mark the area I'm populating and set the spacing to 0. It is then inherited through my table - thus it works for now. But still, it would be nice to be able to control the spacing from C# and not rely on inheritance in Word.

A: 

I've used the built-in style "Table Grid" to remove the paragraph spacing style in the cells (The Word 2007 default, Insert > Table uses the same style):

Word.Document Doc = Globals.ThisDocument.Application.ActiveDocument;
Word.Table WordTable = Doc.Tables.Add(curSel.Range, 8, 5, ref missing, ref missing);

//Table Style
object tableStyle = "Table Grid";
WordTable.set_Style(ref tableStyle);
Mike Regan
@Mike: I tried your solution, but I get an exception at WordTable.set_Style(ref tableStyle);. But I found another solution.
Chau
The exception is likely coming from the style "Table Grid" not being found. I'd caution against making individual formatting changes on every table and instead, creating a table style for all tables with the same formatting.
Mike Regan
A: 

According to Jose Anton Bautista the solution is like the following:

Word.Document currentDocument;
currentDocument.Paragraphs.SpaceAfter = 0;

Or

Word.Table table;
table.Range.Paragraphs.SpaceAfter = 0;

This works very well and to me, it shows where I also can access various properties of the document elements.

Chau