Results 1 to 5 of 5
  1. #1
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Bookmark collection (Word 2002)

    The bookmarks collection sorts bookmarks in alpha order. What would be the best way to cycle through the bookmarks in a document in the order in which they occur, from top of document down?

    Thanks, Richard Barrett

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Bookmark collection (Word 2002)

    You could store the bookmark positions in an array and sort that. The code below uses a two-dimensional array. InitArray fills it, SortArray sorts the index column, and UseArray shows how to use it to loop through the bookmarks in the order in which they occur in the document. The code uses the start position to determine the sort order; you can use the end position of the bookmarks instead if you prefer.

    The Test procedure is just a demo. The two Dim statements at the start should be at the top of a module, after Option Explicit but before all Subs and Functions.

    Dim arrBmk()
    Dim lngBmkCount As Long

    Sub Test()
    InitArray
    SortArray
    UseArray
    End Sub

    Sub InitArray()
    Dim i As Long
    lngBmkCount = ActiveDocument.Bookmarks.Count
    ReDim arrBmk(1 To 3, 1 To lngBmkCount)
    For i = 1 To lngBmkCount
    arrBmk(1, i) = i
    arrBmk(2, i) = ActiveDocument.Bookmarks(i).Start
    Next i
    End Sub

    Sub SortArray()
    Dim i As Long
    Dim j As Long
    Dim lngTmp As Long

    For i = 1 To lngBmkCount - 1
    For j = i + 1 To lngBmkCount
    If arrBmk(2, arrBmk(1, j)) < arrBmk(2, arrBmk(1, i)) Then
    lngTmp = arrBmk(1, i)
    arrBmk(1, i) = arrBmk(1, j)
    arrBmk(1, j) = lngTmp
    End If
    Next j
    Next i
    End Sub

    Sub UseArray()
    Dim i As Long
    For i = 1 To lngBmkCount
    Debug.Print ActiveDocument.Bookmarks(arrBmk(1, i)).Name, _
    ActiveDocument.Bookmarks(arrBmk(1, i)).Start
    Next i
    End Sub

  3. #3
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: Bookmark collection (Word 2002)

    Thanks very much. I had not ever given any thought to the Start property of a bookmark.
    Thanks again, Richard

  4. #4
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: Bookmark collection (Word 2002)

    Hans, I've decided it would make more sense for the user to see a listing of the text within the bookmark range, sorted by that text.

    I've used the following code, with the old WordBasic.SortArray command. This works will for me in Word 2002 if I set with an array filled with Zebra, Cow and Hose, for example.

    If I don't sort the array, my test results (using Debug.Print) are the following:
    5Five (five)
    4Four (four)
    1One (one)
    6Six (six)
    3Three (three)
    2Two (two)

    Once I sort the array, the results are (note the leading space on each line):
    2.63198781758337E-308
    2.63198784369917E-308
    2.63198784369919E-308
    2.63198784369921E-308
    2.63198784369923E-308
    2.63198784369925E-308

    My code is as follows: (I know that all bookmarks are non-empty type)

    Sub RBTest()
    Dim sBookmarks()
    Dim lCount As Long
    Dim x
    Dim bm As Bookmark

    lCount = ActiveDocument.Bookmarks.Count
    If lCount = 0 Then
    MsgBox "There are no bookmarks in this document."
    Exit Sub
    End If

    ReDim sBookmarks(0 To lCount - 1)

    x = 0
    For Each bm In ActiveDocument.Bookmarks
    sBookmarks(x) = bm.Range.Text & " (" & bm.Name & ")"
    x = x + 1
    Next

    WordBasic.SortArray sBookmarks()

    For x = 0 To lCount - 1
    Debug.Print sBookmarks(x)
    Next
    End Sub

    Is there something better than the WordBasic.SortArray command? It seems to work in Word 2002, as I mentioned, when I fill the array with three simple words. But this array gives me trouble.
    Thanks, as always, for your help.
    Regards,
    Richard

  5. #5
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: Bookmark collection (Word 2002)

    Hans, I just discovered the problem with the bit of code I posted to you. I didn't declare the array as a String.... as a result, WordBasic.SortArray got "confused."
    Regards, Richard

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •