在 Word VBA 環境執行,自動選取游標所在的整頁內容(而非全文),方便針對單頁進行格式調整、複製或列印。
Dim CurrentPageStart As Long, CurrentPageEnd As Long, myRange As Range
Dim Currentpage As Integer, Pages As Integer
On Error Resume Next
Currentpage = Selection.Information(wdActiveEndPageNumber)
Pages = Selection.Information(wdNumberOfPagesInDocument)
CurrentPageStart = Selection.GoTo(what:=wdGoToPage, Which:=wdGoToNext, Name:=Currentpage).Start
If Currentpage = Pages Then
CurrentPageEnd = ActiveDocument.Content.End
Else
CurrentPageEnd = Selection.GoTo(what:=wdGoToPage, Which:=wdGoToNext, Name:=Currentpage + 1).Start
End If
Set myRange = ActiveDocument.Range(CurrentPageStart, CurrentPageEnd)
myRange.Select
wdActiveEndPageNumber)與文件總頁數(wdNumberOfPagesInDocument)。Selection.GoTo 移動至目前頁面起始位置(wdGoToNext 配合目前頁碼)。Range 物件涵蓋起迄位置並 Select 選取。