# Word question regarding Bookmarks and VBA



## SydneyGeek (Oct 20, 2008)

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


----------



## SydneyGeek (Oct 21, 2008)

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:

```
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


----------



## Macropod (Oct 22, 2008)

Hi Denis,

There's no need to test every bookmark in the document, just those in/spanning the selection:
	
	
	
	
	
	



```
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
```


----------



## SydneyGeek (Oct 23, 2008)

Aah... (lightbulb symbol)

Thanks Paul. 

Denis


----------



## SydneyGeek (Oct 23, 2008)

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


----------



## Macropod (Oct 23, 2008)

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.


----------



## SydneyGeek (Oct 24, 2008)

Thanks -- I'll try it out

Denis


----------

