Sub 根据表格中文字的行数设置对齐方式()
Dim aCell As Cell, lines, lineStart, lineEnd As Long
For Each aCell In ActiveDocument.Tables(1).Range.Cells
With aCell.Range
.Cells.VerticalAlignment = wdCellAlignVerticalCenter '竖直居中
If .Paragraphs.Count > 1 Then '段落数大于1,则必然多行
.ParagraphFormat.Alignment = wdAlignParagraphJustify '两端对齐
Else
'单元格中只有一个段落,则计算行数
.MoveEnd wdCharacter, -1
lineStart = .Information(wdFirstCharacterLineNumber)
.Collapse wdCollapseEnd
lineEnd = .Information(wdFirstCharacterLineNumber)
lines = lineEnd - lineStart + 1
If lines > 1 Then '多行则水平靠左
.ParagraphFormat.Alignment = wdAlignParagraphJustify '水平靠左
Else '一行则水平居中
.ParagraphFormat.Alignment = wdAlignParagraphCenter '两端对齐
End If
End If
End With
Next
End Sub

你好,能帮忙看一下VBA程序吗?想实现的功能是根据单元内容,设置段落格式,单行为居中,多行或多段为两端对齐,现在程序为图片所示。