我重新作了一个,主要是对于行数的取值,脱离了原有模式,用文件属性中的相应值来取得,你可借鉴一下。 以下代码供参考: '* +++++++++++++++++++++++++++++++++++++++ '* Created By 守柔(ShouRou)@ExcelHome 2005-1-15 13:36:25 '仅测试于System: Windows NT Word: 10.0 Language: 2052 '^The Code CopyIn [ThisDocument-ThisDocument]^' '* -------------------------------------------------------------------------- Sub InsertLineRange()
Dim i As Integer, LineCount As Integer, Doc As Document, StartRange As Long, EndRange As Long
Dim MyRange As Range, TarLines As Integer
On Error GoTo Start
Start: i = InputBox("请输入需要插入的行号")
If i <= 0 Then MsgBox "无效行号,请重新输入!", vbOKOnly + vbInformation: GoTo Start
Application.ScreenUpdating = False
Set Doc = Documents.Open(FileName:=ThisDocument.Path & "\Test.doc", Visible:=False)
ThisDocument.Activate '防止版本不同,加上一句激活本文档
With Doc
TarLines = .BuiltInDocumentProperties("Number of lines").Value 'DOC文档的行数
If i > TarLines Then
MsgBox "大于指定文档最大行号数" & TarLines & " ,请重新输入!", vbOKOnly + vbInformation
.Close False '关闭DOC文档
GoTo Start '返回指定行标签
Else
StartRange = .GoTo(wdGoToLine, , i).Start '指定行号的始点位置
'如果输入行号与DOC的总行数一致,则终点位置为文档末位置,反之则为下一行的起点
EndRange = VBA.IIf(i = TarLines, .Content.End, .GoTo(wdGoToLine, , i + 1).Start)
Set MyRange = .Range(StartRange, EndRange) '定义一个RANGE对象
' MsgBox MyRange
Selection.InsertAfter MyRange '活动文档光标处插入指定行的文本内容
.Close False '关闭文档
End If
End With
Application.ScreenUpdating = True
End Sub
'---------------------- |