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

11
我有一个宏,用于扫描我的文档中的“标题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
2个回答

22
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

3
谢谢,可以了。我有一个非常微小的请求,如果你知道答案 - 尽管选择保持与之前相同,但屏幕会移动以使选择在屏幕顶部。是否有办法也保存屏幕位置?谢谢。 - David Gard
好问题...我建议你发布一个新问题。我不是立即知道的;你会得到更多关注与一个新问题。 - Jean-François Corbett
@DavidGard 滚动位置:https://dev59.com/eIvda4cB1Zd3GeqPetZ2#30765988 - kol
这是否意味着我们无法获取非活动文档的选择? - AKd

2

您还可以使用书签:

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

谢谢您的建议,但似乎并没有起作用。 - David Gard

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接