Consulting

Results 1 to 7 of 7

Thread: Solved: Search bookmarks by location

  1. #1

    Solved: Search bookmarks by location

    Hi,

    Just a quick question. I need to search bookmarks by location in the document. Does anyone know how I would go about doing that?

    The normal:-

    [vba]
    For each bMark in ActiveDocument.Bookmarks
    ..
    ..
    Next
    [/vba]

    Doesn't appear to work as it searches by name rather than location - and searching top/down is essential in what i need to do.

    Thanks in advance.
    Last edited by murphy84; 10-05-2007 at 04:35 AM.

  2. #2
    Think the following works...

    [VBA]ActiveDocument.Range.Bookmarks.DefaultSorting =wdSortByLocation[/VBA]

    Perhaps I should have looked before posting!

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Thanks for posting a followup.

  4. #4
    It looks like my solution doesn't work after all. It's still searching by name.

    Anyone have any ideas what's up? The code i'm using is roughly as follows:-

    [vba]
    ActiveDocument.Bookmarks.DefaultSorting = wdSortByLocation

    For i = 1 To ActiveDocument.Bookmarks.Count
    Set bMark = ActiveDocument.Bookmarks(i)
    ..
    ..
    ..
    Next i
    [/vba]

    Any help would be much appreciated!

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    If you read help:

    Remarks
    This property doesn't affect the order of Bookmark objects in the Bookmarks collection.

    SortByLocation only sorts the order shown in the dialog.

    Perhaps if you tell us exactly what you are trying to do. What exactly do you mean by search by location?

  6. #6

    Smile

    It's okay, I've sorted it now.

    It appears that :-

    [VBA]For Each bMark In ActiveDocument.Bookmarks
    Next[/VBA]

    Searches the bookmarks in name order but...

    [VBA]For Each bMark In ActiveDocument.Range.Bookmarks
    Next[/VBA]

    Searches the bookmarks in location order. Seems to make sense!

    Definately solved now

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Very good. That is an excellent example/demonstration of Range!

    The first refers to the ActiveDocument Bookmark collection, so will be in the order of the document.

    The second refers to the Range Bookmark collection, and therefore will be in the order of the Range, which are numbers (ie. locations).

    It does indeed make sense, and again, is a excellent example of understanding the object model.

Posting Permissions

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