今天围绕多行或多段落的垂直居中问题进行学习,勉强有了处理办法,当然只是不择手段的土办法,代码自然也不够简洁。至于关键的段落行间距与字符大小、页面设置等的关系问题还是无法弄明,Word对此似乎也是秘而不宣。只以初步测试,不知此法是否可靠。我想应会有简便的方法。
Sub Test() Dim Oh As Single, myRange As Range, ll As Long, ww As Long, tt As Long Dim h As Long, h1 As Long, h2 As Long, h3 As Long, h4 As Long, h5 As Long, hh As Long, te As Long On Error GoTo Exit_sub Application.ScreenUpdating = False With Selection If .Type <> wdSelectionShape And .ShapeRange.Count <> 1 Then MsgBox "没有选中文本框或光标不在文本框内!": Exit Sub .Expand wdStory Oh = .ShapeRange.Height .ShapeRange.TextFrame.MarginTop = 0 .ShapeRange.TextFrame.MarginBottom = 0 Set myRange = .Range .StartOf wdStory .MoveDown End With With myRange If Selection.End > 1 Then '如果有两行以上 .Paragraphs(1).SpaceBefore = 0 .Paragraphs.Last.SpaceAfter = 0 ActiveWindow.GetPoint ll, tt, ww, h, myRange If PixelsToPoints(h, True) > Oh Then MsgBox "文本框段落有隐藏,程序退出!": Exit Sub Application.ScreenRefresh ActiveWindow.GetPoint ll, tt, ww, h1, .Paragraphs(1).Range.Characters(1) .Paragraphs(1).LineSpacingRule = wdLineSpaceExactly .Paragraphs(1).LineSpacing = .Paragraphs(1).Range.Font.Size Application.ScreenRefresh ActiveWindow.GetPoint ll, tt, ww, h2, .Paragraphs(1).Range.Characters(1) Undo 2 .Paragraphs.Add Application.ScreenRefresh ActiveWindow.GetPoint ll, tt, ww, h3, .Paragraphs.Last.Previous.Range.Characters.Last .SetRange .Start, .Paragraphs.Last.Previous.Range.End Application.ScreenRefresh ActiveWindow.GetPoint ll, tt, ww, h4, myRange .MoveEnd wdParagraph, 1 .Paragraphs.Last.Previous.LineSpacingRule = wdLineSpaceExactly .Paragraphs.Last.Previous.LineSpacing = .Paragraphs.Last.Previous.Range.Font.Size Application.ScreenRefresh ActiveWindow.GetPoint ll, tt, ww, h5, .Paragraphs.Last.Previous.Range.Characters.Last Undo 3 hh = PixelsToPoints((h1 - h2) / 2, True) te = PixelsToPoints((h3 - h5) / 2, True) tt = PixelsToPoints(h4, True) - hh - te .Paragraphs(1).SpaceBefore = (Oh - tt) / 2 - hh Else '如果只有一行 .Paragraphs(1).LineSpacingRule = wdLineSpaceExactly .Paragraphs(1).LineSpacing = .Paragraphs(1).Range.Font.Size .Paragraphs(1).SpaceBefore = (Oh - .Paragraphs(1).Range.Font.Size) / 2 End If .ParagraphFormat.Alignment = wdAlignParagraphCenter End With Selection.StartOf wdStory Exit Sub Exit_sub: MsgBox "程序意外终止!" Application.ScreenUpdating = True End Sub
[此贴子已经被作者于2008-4-6 23:27:20编辑过] |