Save the cursor position in a document and return there later on

David Gard picture David Gard · Oct 30, 2014 · Viewed 18.1k times · Source

I have a macro that scans my document for Heading 1 styles, and consequently the cursor is moved to after the last match.

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 answer on SO, but it's not working (No error, it just doesn't do anything.)

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

Answer

Jean-François Corbett picture Jean-François Corbett · Oct 30, 2014
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