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 c开发者_开发知识库ells 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?
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?
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.
This is for an old question, but I just ran into the same problem and a fix for this. Add this to your table:
tbl.Range.ParagraphFormat.SpaceAfter = 1; // change the 1 to how much space you want extra in your cell
Just if anybody is following up this post, my text also was arranged at the top of the cells. So for me the following did what I needed in a qick way
oTable.Range.ParagraphFormat.SpaceAfter = 6;
oTable.Range.ParagraphFormat.SpaceBefore = 6;
Space can be adjusted if needed.
The following worked for me in Word 2011 for Mac- nothing else suggested in numerous sites seemed change the vertical cell alignment for me. Found this out by trial and error. I highlighted the cells I wanted vertically align to bottom right and changed the line spacing (in my case it was 1.5 for the table) to 1 for those cells. It finally worked. Hope it helps.
Highlight text within the table --> go to Line Spacing Options --> in the Spacing Before/After section set Before and After to zero px (or equal).
All the alignment options should now work.
精彩评论