2014-10-30 49 views
2

我有一個宏可以掃描我的文檔Heading 1樣式,因此光標會移至上一次匹配後的位置。保存光標在文檔中的位置並在稍後返回

我想在發生此掃描之前捕獲光標的位置,然後在完成後返回到該位置。我該怎麼做呢?

我發現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 

回答

4
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

謝謝你,那是有效的。如果你碰巧知道答案,我確實有一個非常小的請求 - 即使選擇保持原樣,屏幕也會移動,以便選擇位於屏幕的頂部。無論如何也要保存屏幕位置?謝謝。 – 2014-10-30 10:35:51

+0

好問題...我建議你發佈一個新問題。我不知道我的頭頂;你會得到一個新的問題更多的關注。 – 2014-10-30 10:45:13

+0

會做什麼,只是想知道。雖然有點小氣,但是我知道有些用戶會因'屏幕跳躍'而受到驚嚇,所以我只想爲他們節省一些壓力!謝謝。 – 2014-10-30 10:52:11

0

您也可以使用書籤(沒有錯誤,只是沒有做任何事情。):

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

感謝您的建議,但似乎並沒有工作。 – 2014-10-30 12:16:50

相關問題