Results 1 to 7 of 7

Word question regarding Bookmarks and VBA

This is a discussion on Word question regarding Bookmarks and VBA within the General Excel Discussion & Other Questions forums, part of the Question Forums category; I've come up against a problem. My knowledge of the Word object model is sketchy, and I'm trying to find ...

  1. #1
    MrExcel MVP
    Moderator
    SydneyGeek's Avatar
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    12,185

    Default Word question regarding Bookmarks and VBA

    I've come up against a problem. My knowledge of the Word object model is sketchy, and I'm trying to find a way to identify which bookmark I'm at.

    A bit of detail...

    A large word doc has numerous links to an even larger Excel file. I want to replace the links with bookmarks, so that I can loop through them and pull in the corresponding Excel tables.
    I don't think it will be a problem to loop through all of the bookmarks and get the data, but if I'm at a specific bookmark in the middle of this doc, how do I find out which bookmark I'm at? Excel has ActiveSheet, Selection, and several other useful shortcut refs. Is there such a beast for Bookmarks in a Work document?

    Thanks
    Denis
    Self-preservation: For when you've got yourself in a jam
    ------------------------------------------------------
    My site contains a number of Excel and Access Resources

  2. #2
    MrExcel MVP
    Moderator
    SydneyGeek's Avatar
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    12,185

    Default Re: Word question regarding Bookmarks and VBA

    OK, it looks like I have an answer. I can loop through the Bookmarks collection, checking to see which bookmark has the same start point as the current selection.
    Example:
    Code:
    Sub ShowCurrentBookmark()
      Dim bmk As Bokomark
      Dim sName As String
      
      For Each bmk In Activedocument.Bookmarks
        If bmk.Start = Selection.Start Then
          sName = bmk.Name
          Exit For
        End if
      Next bmk
      MsgBox sName
    End Sub
    Denis
    Self-preservation: For when you've got yourself in a jam
    ------------------------------------------------------
    My site contains a number of Excel and Access Resources

  3. #3
    Board Regular
    Join Date
    Aug 2007
    Location
    Canberra, Australia
    Posts
    822

    Default Re: Word question regarding Bookmarks and VBA

    Hi Denis,

    There's no need to test every bookmark in the document, just those in/spanning the selection:
    Code:
    Sub ShowCurrentBookmark()
    Dim bmk As Bookmark
    For Each bmk In Selection.Bookmarks
      MsgBox "Name: " & bmk.Name & vbCrLf & "Text: " & bmk.Range.Text
    Next bmk
    If Selection.Bookmarks.Count = 0 Then MsgBox "No bookmarks in selection."
    End Sub
    Cheers
    Paul Edstein
    [MS MVP - Word]

  4. #4
    MrExcel MVP
    Moderator
    SydneyGeek's Avatar
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    12,185

    Default Re: Word question regarding Bookmarks and VBA

    Aah... (lightbulb symbol)

    Thanks Paul.

    Denis
    Self-preservation: For when you've got yourself in a jam
    ------------------------------------------------------
    My site contains a number of Excel and Access Resources

  5. #5
    MrExcel MVP
    Moderator
    SydneyGeek's Avatar
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    12,185

    Default Re: Word question regarding Bookmarks and VBA

    Just had a thought...

    In the case I was referring to, all of the objects that I am searching are pictures with enclosing bookmarks. Would the internal bookmark search work under those conditions?

    Denis
    Self-preservation: For when you've got yourself in a jam
    ------------------------------------------------------
    My site contains a number of Excel and Access Resources

  6. #6
    Board Regular
    Join Date
    Aug 2007
    Location
    Canberra, Australia
    Posts
    822

    Default Re: Word question regarding Bookmarks and VBA

    Hi Denis,

    I'd expect it work just the same. The only material difference between my code and yours is that mine tests only for the bookmarks in/spanning the selected range.
    Cheers
    Paul Edstein
    [MS MVP - Word]

  7. #7
    MrExcel MVP
    Moderator
    SydneyGeek's Avatar
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    12,185

    Default Re: Word question regarding Bookmarks and VBA

    Thanks -- I'll try it out

    Denis
    Self-preservation: For when you've got yourself in a jam
    ------------------------------------------------------
    My site contains a number of Excel and Access Resources

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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


DMCA.com