vba 将光标位置保存在文档中并稍后返回
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26650244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Save the cursor position in a document and return there later on
提问by David Gard
I have a macro that scans my document for Heading 1
styles, and consequently the cursor is moved to after the last match.
我有一个宏可以扫描我的文档的Heading 1
样式,因此光标会移动到最后一次匹配之后。
I'm trying to capture the location of the cursor before this scan occurs, and then return to that position after it has finished. How do I do this?
我试图在此扫描发生之前捕获光标的位置,然后在完成后返回到该位置。我该怎么做呢?
I found this answeron SO, but it's not working (No error, it just doesn't do anything.)
我在 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
回答by Jean-Fran?ois Corbett
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
回答by Patrick Honorez
You could also use bookmarks:
您还可以使用书签:
Sub test()
ThisDocument.Bookmarks.Add ("xx")
{... do stuff here ...}
ThisDocument.GoTo what:=wdGoToBookmark, Name:="xx"
End Sub