Word question regarding Bookmarks and VBA

SydneyGeek

MrExcel MVP
Joined
Aug 5, 2003
Messages
12,251
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
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,576
Messages
6,120,350
Members
448,956
Latest member
Adamsxl

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top