I believe there is a keyboard shortcut for going back to the last place you were in a document. I haven’t used it in years because it was fairly useless. As soon as you did something in another section of a document, then that became the new ‘last place’ place and you couldn’t make it back to where you were.
Here’s what I’d like to do. Run a macro that puts a placeholder, bookmark, or whatever type of field is appropriate at wherever the cursor is at the moment. Whatever it is it needs to be completely non-intrusive and cause no formatting or other changes to the document (i.e. unless you’re looking for it you’d never know it was there), and not leave any artefacts when the document goes into design (i.e. InDesign or Quark won’t translate it into a printing character). Before placing the field, the macro should also check for and delete other such fields in the document (saying “there can be only one” in its head).
Then I’d want a second macro that simply moves the cursor/view directly to that point.
I don’t know if there’s something built in, but the VBA macros are simple. Here’s to add the bookmark (and delete it if it already exists):
Sub BookmarkAtCursor()
If ActiveDocument.Bookmarks.Exists("LastCursorLoc") Then
ActiveDocument.Bookmarks("LastCursorLoc").Delete
End If
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="LastCursorLoc"
End Sub
And here’s to jump to it:
Sub GotoBookmark()
If ActiveDocument.Bookmarks.Exists("LastCursorLoc") Then
Selection.GoTo What:=wdGoToBookmark, Name:="LastCursorLoc"
End If
End Sub
Wow, fantastic. Just fantastic. This is going to be extraordinarily useful! Thanks so much~
Am I understanding this correctly–that I can add a BookmarkAtCursor_2 macro and name the bookmark it to LastCursorLoc_2 (and make the corresponding changes to GotoBookmark_2) and have two different places to move back to (or as many as I can name)?
The way you assign them will differ by Word version. For Word 2010, right-click in the ribbon and select Customize the Ribbon (or go to Word options through the File menu). On the Customize Ribbon tab, select Macros in “Choose commands from”. You can add it to a custom group on any ribbon.
At the bottom of the same screen, you can also assign keyboard shortcuts to the macros.
It’s that shift/F5 that I tried using long ago. Great if you don’t move around or do much in a document, but its utility falls off the page very quickly.
In addition to assigning macros to the keyboard and ribbon, it’s very handy to map your mouse to the macros. I’m heavily reliant on my 12-button Logitech, and have been drooling over some gaming mice with more. If you have a Logitech mouse, get Uberoptionsto go with it–it’s a fantastic program that replaces SetPoint.
You didn’t ask for it, but you gave me an excuse to blow off work for a little bit. Here are some updated macros that maintain a history of “last positions”, so each time the macro runs it steps back one position. I arbitrarily set it to keep a maximum number of 10 bookmarks in the history, but you can change that.
I’ve done only simple testing of this, so no guarantees.
Option Explicit
Const MAX_SAVED_BOOKMARKS = 10
Sub SetBookmarkAtCursor()
Dim lastBookmark As Long
lastBookmark = SetLastBookmarkIndex()
If lastBookmark > MAX_SAVED_BOOKMARKS Then
If ActiveDocument.Bookmarks.Exists("LastCursorLoc_" & lastBookmark - MAX_SAVED_BOOKMARKS) Then
ActiveDocument.Bookmarks("LastCursorLoc_" & lastBookmark - MAX_SAVED_BOOKMARKS).Delete
End If
End If
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="LastCursorLoc_" & lastBookmark
End Sub
Sub GotoLastBookmark()
Dim startBkmk As Long, endBkmk As Long, i As Long, jump As Boolean
jump = False
endBkmk = GetLastBookmarkIndex()
i = endBkmk
startBkmk = endBkmk - MAX_SAVED_BOOKMARKS
If startBkmk < 2 Then startBkmk = 2
Do While i > startBkmk And i > 1 And Not jump
If ActiveDocument.Bookmarks.Exists("LastCursorLoc_" & i) Then
If ActiveDocument.Bookmarks("\Sel").End = ActiveDocument.Bookmarks("LastCursorLoc_" & i).End Then
If ActiveDocument.Bookmarks.Exists("LastCursorLoc_" & i - 1) Then
Selection.GoTo What:=wdGoToBookmark, Name:="LastCursorLoc_" & i - 1
jump = True
End If
End If
End If
i = i - 1
Loop
If Not jump And ActiveDocument.Bookmarks.Exists("LastCursorLoc_" & endBkmk) Then
Selection.GoTo What:=wdGoToBookmark, Name:="LastCursorLoc_" & endBkmk
End If
End Sub
Function SetLastBookmarkIndex() As Long
SetLastBookmarkIndex = GetLastBookmarkIndex() + 1
ActiveDocument.Variables("LastBookmarkIndex") = SetLastBookmarkIndex
End Function
Function GetLastBookmarkIndex() As Long
' no way to check if variable already exists, so add it if we get an error
On Error Resume Next
GetLastBookmarkIndex = ActiveDocument.Variables("LastBookmarkIndex")
If Err.Number <> 0 Then
GetLastBookmarkIndex = 0
ActiveDocument.Variables.Add Name:="LastBookmarkIndex", Value:=GetLastBookmarkIndex
End If
On Error GoTo 0
End Function
I’ll also note that if you bookmark the same position twice, it short-circuits the history. I.e., if your history is A->B->C->D->B, then going back from position B will always take you to D; you won’t get back to A. I don’t feel inspired to see if I can fix that, so I’m going to say it’s an intended feature.