i want to write a vba macro that read the bookmark name on a selected place in word document.

Hi Halsoft,

I am not sure what you want to do but here goes.

If what you want to do is look through your current selection to see if it contains a bookmark you could try something like this:

Public Sub test()
   Dim i As Integer
   
   For i = 1 To ActiveDocument.Bookmarks.Count
      If ActiveDocument.Bookmarks(i).Start >= Selection.Start And ActiveDocument.Bookmarks(i).End <= Selection.End Then
         'this bookmark is FULLY contained in the selection
         Debug.Print ActiveDocument.Bookmarks(i).Name
      ElseIf ActiveDocument.Bookmarks(i).Start >= Selection.Start And ActiveDocument.Bookmarks(i).End > Selection.End Then
         'this bookmark starts in the selection but finishes after the selection
         Debug.Print ActiveDocument.Bookmarks(i).Name
      ElseIf ActiveDocument.Bookmarks(i).Start < Selection.Start And ActiveDocument.Bookmarks(i).End <= Selection.End Then
         'this bookmark starts before the selection but finishes in the selection
         Debug.Print ActiveDocument.Bookmarks(i).Name
      Else
         'this bookmark is totally OUTSIDE the selection
      End If
   Next
End Sub

Now that you, and I 'cause I just found this, know how to use the .Start and .End you can also use this for any number of things.

However, if you want to do something else then please correct my understanding.

Happy coding

Yomet

hi yomet

its working very good

it save me a time for this weekend

mike bitter
halsoft

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.