问题解决,改了一下版主的代码:
Sub Sample() Dim P As String, PS() As String, PageHome As Integer, PageEnd As Integer, EndPage As Long Dim MyRange As Range, NewDoc As Document Dim MySelection As Selection On Error Resume Next P = "1-1" ' InputBox(prompt:="请在此输入连续页的首页-尾页,以-为分隔符!", Title:="Word连续页选定") If P = "" Then Exit Sub PS = Split(P, "-") '返回一个以"-"分隔的一维数组 If UBound(PS) > 1 Then Exit Sub '如果上标大于1,则退出(用户连续型输入如1-2-7") PageHome = PS(0) '首页为数组下标 PageEnd = PS(1) '尾页为数组上标 If PageHome > PageEnd Then Exit Sub '尾页大于首页则退出 If PageHome < 1 Then Exit Sub '首页小于1则退出 With ActiveDocument 'EndPage为尾页位置,如果大于文档总页数,则为文档最后位置;反之则下一页的起始位置 EndPage = VBA.IIf(PageEnd >= .GoTo(wdGoToPage, wdGoToNext, , PageEnd).Information _ (wdNumberOfPagesInDocument), .Content.End, _ .GoTo(wdGoToPage, wdGoToNext, , PageEnd + 1).Start) '定义一个RANGE对象 ' MySelection.Range = MyRange 'MySelection.Copy ' .ActiveWindow.Visible = False Set MyRange = .Range(.GoTo(wdGoToPage, wdGoToNext, , PageHome).Start, EndPage) MyRange.Select .ActiveWindow.Selection.Copy '.ActiveWindow.Selection.CopyAsPicture'可以生成图片
'If MsgBox("您想要将指定页的内容另存为WORD文件吗?", vbOKCancel + vbInformation) = vbOK Then 'Set MySelection.Range = MyRange Set NewDoc = Documents.Add '定义一个新文档 With NewDoc '.Content = MyRange '将MyRange写入新文档中 '打开另存为对话框 .ActiveWindow.Selection.Paste '.Range = MyRange.Duplicate '.Range.Find = MyRange.Font '.Range.Duplicate = MyRange.Duplicate ' Application.Dialogs(wdDialogFileSaveAs).Show 'NewDoc.Close '关闭新文档 End With ' End If '.ActiveWindow.Visible = True End With End Sub
|