Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 379
David Gard Avatar asked Oct 30 '14 10:10

David Gard


People also ask

How do I go back to the previous cursor position in Word?

All you have to do is press the space bar and Word immediately returns you to the insertion point.

How do you jump to a cursor in Word?

The easiest way to make Word jump to the last cursor position in Word documents is to use the keyboard shortcut [Shift F5].


1 Answers

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
like image 166
Jean-François Corbett Avatar answered Jan 02 '23 16:01

Jean-François Corbett