Auto-open Word document at last postition

I’ve been working with a larger Word document a lot last few days and I got really tired of having to scroll to the last position where I was before. I was about to write a macro, but alas, it’s been written already! It makes Word remember your last editing position…

The only trouble with that macro is that it leaves visible characters in the document (fourteen asterixes in this case) that you don’t want there. So I’ve improved it a little bit. It now hiddes the “bookmarking characters”:

Sub AutoOpen()

‘ GoToMyBookmarkText


On Error Resume Next

Selection.Find.ClearFormatting
With Selection.Find
.Text = “**************”
.Replacement.Text = “”
.Forward = True
.Wrap = wdFindContinue
.Format = True
.Font.Hidden = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
If Selection = “**************” Then
Selection.Delete
End If

End Sub
Sub AutoClose()

‘ CreateMyBookmark


Selection.TypeText Text:=”**************”
Selection.MoveLeft Unit:=wdCharacter, Count:=14, Extend:=wdExtend
With Selection.Font
.Hidden = True
End With
End Sub

But that got me thinking: Why not use Word’s bookmarks! Makes for cleaner code and much cleaner document!

All you need to do to get it working for all your documents is to (I am using Word 2007, might be slightly different in other versions):

  1. go to the View tab on the ribbon
  2. open the Macros dialog
  3. click Create. New windows opens. If there is any text, go to the very bottom.
  4. copy and paste the code bellow and save

Sub AutoClose()
‘ Auto set last cursor position bookmark
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:=”MyLastKnownPosition”
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
End Sub

Sub AutoOpen()
‘ Find last cursor position bookmark, set cursor, and delete the bookmark
If ActiveDocument.Bookmarks.Exists(“MyLastKnownPosition”) = True Then
Selection.GoTo What:=wdGoToBookmark, Name:=”MyLastKnownPosition”
ActiveDocument.Bookmarks(“MyLastKnownPosition”).Delete
With ActiveDocument.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
End If

End Sub

Enjoy!

Leave a Reply