将光标位置保存在文档中,稍后返回

Dav*_*ard 5 vba word-vba word-2010

我有一个宏来扫描我的文档的Heading 1样式,因此光标移动到最后一个匹配后.

我试图在扫描发生之前捕获光标的位置,然后在完成后返回到该位置.我该怎么做呢?

我在SO上找到了这个答案,但它没有用(没有错误,它什么也没做.)

Application.ScreenUpdating = False ' Turn screen updates off so the user will not know that the cursor has moved

Dim currentPosition As Long
currentPosition = Selection.Range.Start

{... do stuff here ...}

Selection.Range.Start = currentPosition

Application.ScreenUpdating = True
Run Code Online (Sandbox Code Playgroud)

Jea*_*ett 8

Dim currentPosition As Range
Set currentPosition = Selection.Range 'pick up current cursor position

' do stuff — cursor gets moved around

currentPosition.Select 'return cursor to original position
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,这很有效.我确实有一个非常小的请求,如果您碰巧知道答案 - 即使选择保持原样,屏幕也会移动,以便选择位于屏幕顶部.反正还有保存屏幕位置吗?谢谢. (3认同)