Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get text in a Word table to vertical align?

I am creating a Word document on the fly as a C# VS 2010 Office Word project for a client who wants to be able to generate a document that will allow the appropriate number of signatory locations for a particular deal going down. There is a table that will need to be generated with sufficient rows and then later in the doc I have to produce prefab blocks for personal info per signatory.

I am working on the table part now and have almost everything as I want it, but the text in all of the cells is vertically top aligned. I have visited EVERY site in the ENTIRE internet in the past few days for up-to-date information on Word automation that is current for .Net 4, VS 2010 and Office 2010. I have syntax that compiles w/o error but fails to bottom align as I desire. I have even stabbed about with IntelliSense to see if I could find another solution.

This code focuses on a single row:
tbl.Range.Rows[1].Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalBottom; This runs but the text stays helium-filled.

Any Word automation wizards out there?

like image 541
leemid Avatar asked Feb 23 '11 19:02

leemid


People also ask

How do you center text vertically in a table cell in Word?

Method One: Right-click and choose “Table Properties.” Go to the Cell tab and choose “Center” below Vertical Alignment. Click “OK.”

How can you vertically align the text?

Select the text that you want to center. in the Page Setup group, and then click the Layout tab. In the Vertical alignment box, click Center. In the Apply to box, click Selected text, and then click OK.


2 Answers

I was unable to reproduce the problem. This code works just fine:

using Microsoft.Office.Interop.Word;
class Program
{
    static void Main(string[] args)
    {
        DocumentClass document = new DocumentClass();
        object defaultTableBehavior = null, autoFitBehavior = null;
        Table tbl = document.Content.Tables.Add(document.Content, 2, 2, ref defaultTableBehavior,
            ref autoFitBehavior);
        tbl.Rows[2].Cells[2].Range.InsertAfter("This is a test.");
        tbl.Rows[2].Cells[2].Range.ParagraphFormat.SpaceAfter = 0f;
        tbl.Rows[2].Cells.Height = 50f;
        tbl.Range.Rows[2].Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalBottom;
    }
}

I suspect that some other problem must be in play, like the paragraph spacing after, or perhaps the wrong range is selected?

like image 107
Jeffrey L Whitledge Avatar answered Oct 19 '22 03:10

Jeffrey L Whitledge


The text is probably centered vertically, but it incudes a paragraph spacing other than "0." So, Word is viewing the extra line as additional text that needs to be included in the vertical centering.

To get around this, simply highlight the text you want to be vertically centered (or the entire table if that is what you want). Then go to "Page Layout" and reduce the "Spacing" "After" to "0." If you also have a space on the top of your text, you will need to reduce the "Spacing" "Before" to "0" as well. With no spacing before or after the text, the actual text will now be centered.

like image 4
NJ Ron Avatar answered Oct 19 '22 02:10

NJ Ron