Sub CopySel()
Dim PageSel As String, i As Cell, PageNumber As Integer, MyNumber As Integer, StartPage As Integer, EndPage As Integer
Dim MyRange As Range, Range2 As Range, StartRange As Long, EndRange As Long, n As Integer, PageCount As Integer
On Error GoTo errhandel
PageCount = Selection.Information(wdNumberOfPagesInDocument)
PageSel = InputBox("请输入需要定位的页数!,号表示非连续页,-号表示连续页", "Microsoft Word")
If PageSel = "" Then Exit Sub
Application.ScreenUpdating = False
Documents.Add
ActiveDocument.SaveAs FileName:="temp1.doc"
Selection.InsertAfter PageSel
Application.DefaultTableSeparator = ","
Selection.ConvertToTable Separator:=wdSeparateByDefaultListSeparator
Documents.Add
ActiveDocument.SaveAs FileName:="temp2.doc"
For Each i In Documents("temp1.doc").Tables(1).Range.Cells
If i.Range.Text Like "*-*" = True Then
For n = 1 To Len(i.Range.Text) - 1
If Mid(i.Range.Text, n, 1) = "-" Then
MyNumber = n - 1
StartPage = CInt(Mid(i.Range.Text, 1, MyNumber))
If StartPage >= PageCount Then GoTo errhandel
EndPage = CInt(Mid(i.Range.Text, MyNumber + 2, Len(i.Range.Text) - 1 - MyNumber - 2))
With Documents("test.doc")
.Activate
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=StartPage
StartRange = Selection.Start
If EndPage = PageCount Then
Selection.EndKey Unit:=wdStory
EndRange = Selection.Start
Else
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=EndPage + 1
EndRange = Selection.Start
End If
Set MyRange = Range(Start:=StartRange, End:=EndRange)
MyRange.Select
Selection.Copy
Set Range2 = Documents("temp2.doc").Content
Range2.Collapse Direction:=wdCollapseEnd
Range2.Paste
End With
Exit For
End If
Next
Else
PageNumber = CInt(Mid(i.Range.Text, 1, Len(i.Range.Text) - 1))
If StartPage > PageCount Then GoTo errhandel
With Documents("test.doc")
.Activate
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=PageNumber
StartRange = Selection.Start
If PageNumber = PageCount Then
Selection.EndKey Unit:=wdStory
EndRange = Selection.Start
Else
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=PageNumber + 1
EndRange = Selection.Start
End If
Set MyRange = Range(Start:=StartRange, End:=EndRange)
MyRange.Select
Selection.Copy
Set Range2 = Documents("temp2.doc").Content
Range2.Collapse Direction:=wdCollapseEnd
Range2.Paste
End With
End If
Next
Documents("temp1.doc").Close (False)
Application.ScreenUpdating = True
Exit Sub
errhandel:
Documents("temp1.doc").Close (False)
Documents("temp2.doc").Close (False)
MsgBox "请检查页号或者页码输入是否有误,或者超出文档的范围!" End Sub
TO:henterwu: 受活动文档限制及一个中间文档的限制,该程序只限于文件级宏中使用,不宜用于全局宏中。 另外你说的最后一页确实存在这个问题已作修改请另外再测试一下。 此程序的要点是判断INPUTBOX 中的输出无论有多少“," 及"-" 号,均在"temp1.doc" 中, 以文字转换为表格的形式将其分开这是该程序的精华所在否则无法作出正确的判断到底有几页和连续的有几页(通过FOR EACH 在表格间循环并获得相应页数当得到相应页数后进行定位取得用户的选择性页号范围,MYRANGE=该页的起始RANGE位置和下一页的起始位置即该页的MYRANGE范围. |