重新打开 Word 文档至先前的光标位置,页面垂直位置相同

重新打开 Word 文档至先前的光标位置,页面垂直位置相同

我想返回到上一个编辑点,页面的垂直位置与原始垂直位置相同。Shift+F5 技术可让我返回到上一个编辑点,但我返回的页面在屏幕上的垂直位置有点随机。换句话说,如果我的光标距离文档视口顶部 300 个垂直像素,我想重新打开页面,使光标的位置仍然距离我的视口顶部 300 个垂直像素。

以下可用于确定我的文本光标的垂直位置(在屏幕上):

ActiveWindow.GetPoint pLeft、pTop、pWidth、pHeight、Selection.Range

因此,挑战就变成了如何滚动我的文档以使我的文本光标返回到其原始垂直位置(pHeight)?

没有相应的 ActiveWindow.SetPoint 和 ActiveWindow.ScrollIntoView 将选择范围滚动到视图中,但无法控制选择范围在屏幕上的垂直位置。

答案1

您可以使用这两个宏来实现这一点。一个用于存储当前视图设置,另一个用于恢复它们。更多代码帮助可用这里这里

编辑:我已更新代码,除了视图类型外,还存储水平和垂直滚动百分比以及当前选择点。我目前没有 Word 副本来测试这一点。

保存视图

Sub StoreDocView()

   ' Declare variables
   Dim OriginalViewType As Long
   Dim OriginalHorizontalScrollPercent as Long
   Dim OriginalVerticalScrollPercent as Long
   Dim OriginalSelectionStart as Long
   Dim OriginalSelectionEnd as Long

   ' Capture the current settings
   OriginalViewType = ActiveWindow.ActivePane.View.Type
   OriginalHorizontalScrollPercent = ActiveWindow.ActivePane.HorizontalPercentScrolled
   OriginalVerticalScrollPercent = ActiveWindow.ActivePane.VerticalPercentScrolled
   OriginalSelectionStart = ActiveWindow.ActivePane.Selection.Start
   OriginalSelectionEnd = ActiveWindow.ActivePane.Selection.End


   ' Store the view information to a Document Variable
   ActiveDocument.Variables.Add Name:="DocViewType", Value:=OriginalViewType
   ActiveDocument.Variables.Add Name:="DocHorizontalScroll", Value:=OriginalHorizontalScrollPercent
   ActiveDocument.Variables.Add Name:="DocVerticalScroll", Value:=OriginalVerticalScrollPercent
   ActiveDocument.Variables.Add Name:="DocSelectionStart", Value:=OriginalSelectionStart
   ActiveDocument.Variables.Add Name:="DocSelectionEnd", Value:=OriginalSelectionEnd

End Sub

检索视图

Sub GetDocView()

   ' Declare variables
   Dim OriginalViewType As Long
   Dim OriginalHorizontalScrollPercent as Long
   Dim OriginalVerticalScrollPercent as Long
   Dim OriginalSelectionStart as Long
   Dim OriginalSelectionEnd as Long

   ' Get document variables
   OriginalViewType = ActiveDocument.Variables("DocViewType").Value
   OriginalHorizontalScrollPercent = ActiveDocument.Variables("DocHorizontalScroll").Value
   OriginalVerticalScrollPercent = ActiveDocument.Variables("DocVerticalScroll").Value
   OriginalSelectionStart = ActiveDocument.Variables("DocSelectionStart").Value
   OriginalSelectionEnd = ActiveDocument.Variables("DocSelectionEnd").Value

   ' Restore settings
   ActiveWindow.ActivePane.View.Type = OriginalViewType
   ActiveWindow.ActivePane.HorizontalPercentScrolled = OriginalHorizontalScrollPercent
   ActiveWindow.ActivePane.VerticalPercentScrolled = OriginalVerticalScrollPercent
   ActiveWindow.ActivePane.Selection.Start = OriginalSelectionStart
   ActiveWindow.ActivePane.Selection.End = OriginalSelectionEnd


End Sub

相关内容