vba 如何在 Word 中使用宏创建的表格中居中文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19492962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How To Center Text in a table that is created using a macro in Word
提问by user2791146
I have a macro that creates a table with 2 columns. I want to center the text.
我有一个宏可以创建一个包含 2 列的表。我想将文本居中。
I need to know the actual function/method to do this (i.e not recorded) as I am editing a complex macro in a specific tool outside of Microsoft Word.
我需要知道执行此操作的实际功能/方法(即未记录),因为我正在 Microsoft Word 之外的特定工具中编辑复杂的宏。
Function TableStyleApply(oTable)
Const wdLineWidth050pt = 4
Const wdLineStyleSingle = 1
Const wdBorderTop = -1
Const wdBorderLeft = -2
Const wdBorderBottom = -3
Const wdBorderRight = -4
Const wdBorderHorizontal = -5
Const wdBorderVertical = -6
Const wdAlignParagraphCenter = 100
oTable.Borders(wdBorderTop ).LineStyle = wdLineStyleSingle
oTable.Borders(wdBorderLeft ).LineStyle = wdLineStyleSingle
oTable.Borders(wdBorderBottom ).LineStyle = wdLineStyleSingle
oTable.Borders(wdBorderRight ).LineStyle = wdLineStyleSingle
oTable.Borders(wdBorderHorizontal).LineStyle = wdLineStyleSingle
oTable.Borders(wdBorderVertical).LineStyle = wdLineStyleSingle
oTable.Rows(1).Range.Font.Bold = True
oTable.Rows(1).Shading.BackgroundPatternColor = 15132390
oTable.Rows.LeftIndent = 43
oTable.Columns(1).SetWidth 280, 2
oTable.Columns(2).SetWidth 157, 2
oTable.Columns.ParagraphFormat.Alignment = wdAlignParagraphCenter
End Function
回答by Kazimierz Jawor
You need to refer to any Range object if you want to align your text in center. So, try with this options
如果要将文本居中对齐,则需要引用任何 Range 对象。所以,试试这个选项
For whole table
对于整桌
oTable.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
For any single column(here, for 1st and 2nd columns)
对于任何单列(此处为第一列和第二列)
oTable.Columns(1).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
oTable.Columns(2).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
回答by Jake Burdsall
This line set alignment to center for all cells in a specific table:
此行将特定表格中所有单元格的对齐方式设置为居中:
cDoc.Tables(1).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter