请参此贴: Dim LineCount As Integer Sub LinesCount() Dim l As String On Error Resume Next If Selection.Type = wdSelectionIP Then Selection.Paragraphs(1).Range.Select Application.ScreenUpdating = False CommandBars("Word Count").Visible = True CommandBars("Word Count").Controls(2).Execute l = CommandBars("Word Count").Controls(1).List(6) CommandBars("Word Count").Visible = False Application.ScreenUpdating = True LineCount = Int(Mid(l, 1, Len(l) - 1)) MsgBox "Selection Paragraphs(1)'s Line Count Is " & LineCount MsgBox NumlineRange(LineNumber) End Sub Function NumlineRange(LineNumber As Variant) As Range Dim StRange As Long, EnRange As Long, SelStart As Range 0 LineNumber = InputBox("请输入你要定位的指定段落的行号", "Microsoft Word") If LineNumber = "" Then Exit Function Else LineNumber = LineNumber * 1 With Selection Set SelStart = ActiveDocument.Range(.Paragraphs(1).Range.Start, .Paragraphs(1).Range.Start) Select Case LineNumber Case 0, Is > LineCount MsgBox "行号过大或者过小的无效行号错误!", vbOKOnly + vbInformation GoTo 0 Case 1 StRange = .Paragraphs(1).Range.Start EnRange = .GoTo(what:=wdGoToLine, which:=wdGoToNext, Count:=LineNumber).Start Case Is = LineCount StRange = .GoTo(what:=wdGoToLine, which:=wdGoToNext, Count:=LineNumber - 1).Start EnRange = .Paragraphs(1).Range.End Case Else StRange = .GoTo(what:=wdGoToLine, which:=wdGoToNext, Count:=LineNumber - 1).Start SelStart.Select EnRange = .GoTo(what:=wdGoToLine, which:=wdGoToNext, Count:=LineNumber).Start End Select Set NumlineRange = ActiveDocument.Range(StRange, EnRange) End With End Function 在这里我着重使用了RANGE对象和GOTO方法也可以使用MOVE的方法进行. 在第一个过程中使用工具字数统计功能分离出所选段落的行数. 在第二个过程中采用的函数法返回指定行的RANGE对象. 同样类似的方法可以将它返回到文档中的任意行的RANGE对象. |