Navigate between Bookmarks in MS Word

Post your working scripts, libraries and tools for AHK v1.1 and older
freespacing
Posts: 150
Joined: 28 Sep 2016, 11:14
Contact:

Navigate between Bookmarks in MS Word

Post by freespacing » 02 Aug 2020, 09:00

A script by Andy on the old forum inspired me to write something to jump between bookmarks in Microsoft Word.

Ctrl + Up / Down are supposed to jump to previous / last bookmarks.

It kind of works: we move location in the file, but MS Word's numbering of bookmarks doesn't seem to reflect the ordering in the file itself. If anyone has ideas on how to improve, that would be fantastic.

Code: Select all

#IfWinActive ahk_exe winword.exe
; Navigate to next and previous bookmarks

^Up::  ; jump to previous bookmark
oWord :=ComObjActive("Word.Application") 
bookmark_count := oWord.ActiveDocument.Bookmarks.count
if(! bookmark_count) { ; first use of the script: jump to first
   current_bookmark := 1
   oWord.ActiveDocument.Bookmarks(Index := 1).select
}
else {
   first_bookmark := (current_bookmark == 1)
   if (first_bookmark) { 
      oWord.ActiveDocument.Bookmarks(Index := bookmark_count).select  ; jump to last
      current_bookmark := bookmark_count  
   }
   else {
   	current_bookmark--
   	oWord.ActiveDocument.Bookmarks(Index := current_bookmark).select 
   }
}
oWord := ""
return

^Down:: ; jump to next bookmark
oWord :=ComObjActive("Word.Application") 
bookmark_count := oWord.ActiveDocument.Bookmarks.count 

if(! bookmark_count) { ; first use of the script: jump to last
   current_bookmark := bookmark_count 
   oWord.ActiveDocument.Bookmarks(Index := current_bookmark).select
}
else {
   last_bookmark := (current_bookmark == bookmark_count)
	if (last_bookmark) { 
		oWord.ActiveDocument.Bookmarks(Index := 1).select  ; jump to first 
		current_bookmark := 1
   }
	else {
 		current_bookmark++
		oWord.ActiveDocument.Bookmarks(Index := current_bookmark).select
	}
}
oWord := ""
return

#IfWinActive


gibbons6546
Posts: 31
Joined: 07 Apr 2020, 11:53

Re: Navigate between Bookmarks in MS Word

Post by gibbons6546 » 15 Aug 2020, 11:22

Hi, thanks for this.
User avatar
gwarble
Posts: 525
Joined: 30 Sep 2013, 15:01

Re: Navigate between Bookmarks in MS Word

Post by gwarble » 15 Aug 2020, 12:34

nice, thanks for sharing

i would have assumed the order of the bookmarks would be the order created since word doesn’t give you an option to reorder them

if this is unpredictable you could iterate through them all and use Bookmark.Range.Start to find the one closest to your current cursor position or order them yourself based on that property

this doesn't work right, but along these lines (would be better to order them once maybe?):

Code: Select all

^Down::
 count := Doc.Bookmarks.count
 Loop, %count%
  If (Doc.Bookmarks(A_Index).Range.Start > Doc.ActiveWindow.Selection.Range.Start)
  {
   Difference := Doc.Bookmarks(A_Index).Range.Start - Doc.ActiveWindow.Selection.Range.Start
   If (Difference < BestDiff) or (A_Index = 1)
    BestIndex := A_Index
    , BestDiff := Difference
  }
 If (BestIndex >= 1)
  Doc.Bookmarks(BestIndex).Select
Return
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .
freespacing
Posts: 150
Joined: 28 Sep 2016, 11:14
Contact:

Re: Navigate between Bookmarks in MS Word

Post by freespacing » 21 Aug 2020, 13:30

Hi @gwarble, thank you for sharing your ideas on this, really appreciate them.
Will give it more thought when I find some time. Fortunately not needing to do much in MS Word.
It saddens me that after 30+ years on the market they haven't thought to implement something as useful as this!
Wishing you a great weekend.
Post Reply

Return to “Scripts and Functions (v1)”