Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove spacing after paragraph in Word 2007 with VSTO

Tags:

c#

ms-word

vsto

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.

like image 937
Chau Avatar asked Feb 16 '10 12:02

Chau


People also ask

How do you remove the space after a paragraph in Word 2007?

Select the text that you want to change. On the Home tab, click the Font Dialog Box Launcher, and then click the Advanced tab. Note: If you're using Word 2007 the tab is called Character Spacing. In the Spacing box, click Expanded or Condensed, and then specify how much space you want in the By box.

How do I get rid of the automatic space after a paragraph in Word?

Click anywhere in the paragraph you want to change. Go to Layout, and under Spacing, click the up or down arrows to adjust the distance before or after the paragraph. You can also type a number directly.

How do you remove spaces after a paragraph?

Select the paragraph or paragraphs you want to format. On the Home tab, click the Line and Paragraph Spacing command. Click Add Space Before Paragraph or Remove Space After Paragraph from the drop-down menu.


1 Answers

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.

like image 121
Chau Avatar answered Sep 20 '22 11:09

Chau