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

时间:2014-10-30 10:12:25

标签: vba word-vba word-2010

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

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

我在SO上找到了this answer,但它没有用(没有错误,它没有做任何事情。)

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

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:0)

您也可以使用书签:

Sub test()
    ThisDocument.Bookmarks.Add ("xx")
    {... do stuff here ...}
    ThisDocument.GoTo what:=wdGoToBookmark, Name:="xx"
End Sub