none
find bookmarks RRS feed

Answers

  • Like this:

    Sub ListBkMrks()
    Dim oBkMrk As Bookmark
    If ActiveDocument.Bookmarks.Count > 0 Then
    With Selection
    .EndKey wdStory
    .TypeText vbCrLf & "Bookmark" & vbTab & "Page" & vbTab & "Contents"
    For Each oBkMrk In ActiveDocument.Bookmarks
    .TypeText vbCrLf & oBkMrk.Name & vbTab
    .Fields.Add Selection.Range, wdFieldPageRef, oBkMrk.Name, False
    .TypeText vbTab & oBkMrk.Range.Text
    Next oBkMrk
    End With
    End If
    End Sub

    The above macro loops through all the bookmarks and outputs their names, page #s and contents at the end of the document.


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Tuesday, May 3, 2011 1:39 AM
  • Hi Michael,

    You’re asking a very broad question.  We can probably be more helpful if you told us what programming language you're using and what are you're trying to do once you’ve got the bookmarked location.

    With that said, below is one method written in VBA.

    Sub FindBookMarks()

        Dim bkMark As Word.Bookmark

        Dim doc As Document

        Set doc = ActiveDocument

        For Each bkMark In doc.Bookmarks

            'do something

        Next

    End Sub

     


    Regards, Rich
    Tuesday, May 3, 2011 1:44 AM

All replies

  • Like this:

    Sub ListBkMrks()
    Dim oBkMrk As Bookmark
    If ActiveDocument.Bookmarks.Count > 0 Then
    With Selection
    .EndKey wdStory
    .TypeText vbCrLf & "Bookmark" & vbTab & "Page" & vbTab & "Contents"
    For Each oBkMrk In ActiveDocument.Bookmarks
    .TypeText vbCrLf & oBkMrk.Name & vbTab
    .Fields.Add Selection.Range, wdFieldPageRef, oBkMrk.Name, False
    .TypeText vbTab & oBkMrk.Range.Text
    Next oBkMrk
    End With
    End If
    End Sub

    The above macro loops through all the bookmarks and outputs their names, page #s and contents at the end of the document.


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Tuesday, May 3, 2011 1:39 AM
  • Hi Michael,

    You’re asking a very broad question.  We can probably be more helpful if you told us what programming language you're using and what are you're trying to do once you’ve got the bookmarked location.

    With that said, below is one method written in VBA.

    Sub FindBookMarks()

        Dim bkMark As Word.Bookmark

        Dim doc As Document

        Set doc = ActiveDocument

        For Each bkMark In doc.Bookmarks

            'do something

        Next

    End Sub

     


    Regards, Rich
    Tuesday, May 3, 2011 1:44 AM
  • I'm using C# and only need to print a particular section of the document.
    Tuesday, May 3, 2011 3:15 AM
  • Hi,

    I have just a problem with "do something".

    I want to go to all bookmarks and to insert some text (from Excel).

            If ActiveDocument.Bookmarks.Exists(ActiveSheet.Cells(RowCnt, 1).Text) = True Then
                ActiveDocument.Bookmarks.Item(ActiveSheet.Cells(RowCnt, 1).Text).Select
                Selection.GoTo What:=wdGoToBookmark, Name:=ActiveSheet.Cells(RowCnt, 1).Text
                Selection.TypeText Text:=ActiveSheet.Cells(RowCnt, 2).Text
            End If

    This works fine, when the bookmark is in the "main" document. It fails in headers, footers and textfields (goto fails).

    Any idea to access realy all bookmarks?

     

    Regards, Detlef

    Tuesday, May 3, 2011 12:52 PM
  • The code posted previously in this thread has nothing to do with updating bookmarked ranges - it's about iterating through the document's bookmarks.

    If you want to update a bookmarked range, you should use code like:

    In Excel:

    Call UpdateBookmark (ActiveSheet.Cells(RowCnt, 1).Text, ActiveSheet.Cells(RowCnt, 2).Text)

    Sub UpdateBookmark (BmkNm as string, NewTxt as string)
    Dim BmkRng as Word.Range
    With ActiveDocument
      If.Bookmarks.Exists(BmkNm) Then
        Set BmkRng =.Bookmarks(BmkNm).Range
        BmkRng.Text = NewTxt
        .Bookmarks.Add BmkNm, BmkRng
      End if
    End With
    Set BmkRng = Nothing
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]
    • Proposed as answer by peterbevsh Friday, May 6, 2011 6:26 PM
    Tuesday, May 3, 2011 8:44 PM