• Bookmark rpt (2003)

    Author
    Topic
    #420561

    Hi All,

    I’m looking for functionality (VBA, add-in, …?) that will give me information about bookmarks in a document. The info I’d be looking for is:
    – bookmark name (rqd)
    – some precise specification of where the bookmark was defined: at least a page# for those bookmarks that only specify a location but more precise location identification would be better; for those bookmarks that define a range, the page#(s) or text values of the range (rqd to have something but somewhat flexible on what is provided here)
    – some precise specification of the location(s) where the bookmark is referenced – precise specification should be at least the page # but para or surrounding text would be better (rqd)
    – the type of reference – is the ref to the bookmark’s page #, text, etc (real nice to have)

    Didn’t see anything on the lounge that came close. Anyone know of anything?

    TIA

    Fred

    Viewing 1 reply thread
    Author
    Replies
    • #952677

      Hi Fred,

      I’ve not heard of anything like that, but you piqued my interest. The following is a rough attempt at most of the functionality you asked for. The collection colBkData contains all the data you’re looking for. For this example, I just dump it out into a new document, but you could of course put it in a table, or otherwise format it.

      Sub GetBookmarkInfo()
      Dim bk As Bookmark
      Dim colBkData As Collection
      Dim col As Collection
      Dim colRefs As Collection
      Dim doc As Document
      Dim strOutput As String
      Dim k As Long
      Dim docReport As Document
      
      Set colBkData = New Collection
      Set doc = ActiveDocument
      doc.Bookmarks.ShowHidden = True
      
      For Each bk In doc.Bookmarks
          Set col = New Collection
          col.Add Key:="name", Item:=bk.Name
          col.Add Key:="page", Item:=bk.Range.Information(wdActiveEndPageNumber)
          col.Add Key:="para-text", Item:=bk.Range.Paragraphs(1).Range.Text
          Set colRefs = FindRefsToBookmark(bk)
          col.Add Key:="refs", Item:=colRefs
          colBkData.Add Item:=col
          Set col = Nothing
      Next bk
      Dim j As Integer
      For k = 1 To colBkData.count
          strOutput = strOutput & _
                 "Bookmark Name: " & colBkData(k)("name") & vbCr & _
                 "Appears on page: " & colBkData(k)("page") & vbCr & _
                 "Surrounding text: " & colBkData(k)("para-text") & vbCr
          If Not colBkData(k)("refs") Is Nothing Then
              For j = 1 To colBkData(k)("refs").count
                  strOutput = strOutput & _
                     "Referenced on page: " & colBkData(k)("refs")(j)("page") & vbCr & _
                      "In the text: " & colBkData(k)("refs")(j)("para-text") & _
                      vbCr & vbCr
              Next j
          End If
      Next k
      Set docReport = Documents.Add
      docReport.Range.InsertAfter strOutput
      End Sub
      '
      Function FindRefsToBookmark(bk As Bookmark) As Collection
      Dim f As Field
      Dim doc As Document
      Set doc = bk.Parent
      Dim colRefData As Collection
      Dim col As Collection
      Set colRefData = New Collection
      
      For Each f In doc.Fields
          Set col = New Collection
          If f.Type = wdFieldRef Then
              If Split(Trim(f.Code))(1) = bk.Name Then
                  f.Select
                  col.Add Key:="page", Item:=Selection.Information(wdActiveEndPageNumber)
                  col.Add Key:="para-text", Item:=Selection.Paragraphs(1).Range.Text
                  colRefData.Add Item:=col
              End If
          End If
          Set col = Nothing
      Next f
      If colRefData.count = 0 Then
          Set FindRefsToBookmark = Nothing
      Else
          Set FindRefsToBookmark = colRefData
      End If
      End Function
      
      • #952735

        Hi Andrew,

        Thanks much. I gave it a whirl and it works great (except that I had to select the document first before running your macro).

        I have a 60-pg document with lots of bookmarks. Some are there to allow me to navigate back to certain spots of the document. I would not expect any of these to be referenced from anywhere else in the doc. And so they weren’t.

        However, there are many bookmarks that are used as part of a TOA entry in case the item spans more than a page. Of course, these bookmarks are part of the hidden text for the TA field. A quick check of 2 of my bookmarks in your “report” shows that neither of these had any Reference info in your report. But they are picked up in the TOA showing a span of pages (eg, “1-2”). The bookmarks that did show up in your “Referenced” info were just a few that were used in a different way (eg, to refer to specific text). The ones that did not show up were ones where I referenced a page # that the bookmarked text was on (eg, “see page x” where x is a cross-ref to a bookmark’s page #).

        I also need to check the hidden bookmarks in terms of what they’re referring to but that seems ok.

        Thanks.

        Fred

        • #952762

          To pick up bookmarks within hidden text, you’d probably have to un-hide the text temporarily. As for refs to page numbers, are your page numbers in the footer? You may need to wrap another For..Each loop around the one that searches for fields to search every StoryRange so it’ll pick up those page fields.

          Glad to hear the basic algorithm is sound — hope you get it working to your specs.

          • #952891

            Hi Andrew,

            >To pick up bookmarks within hidden text, you’d probably have to un-hide the text temporarily.
            These are r switches within a TA field. Is what you’re suggesting to do
            – a select all at the beginning and unhide any hidden text (only text that I have hidden is the {TA…} field. Can do.
            – go thru your macro as previously supplied
            – select all and re-hide TA fields. Not sure how to do this last – search for all { TA…} and hide. (I know the curly braces are not typed on the keyboard.)

            >As for refs to page numbers, are your page numbers in the footer? You may need to wrap another For..Each loop around the one that searches for fields to search every StoryRange so it’ll pick up those page fields.
            Really not sure what you’re suggesting here. I do have my page #s in a footer. What I was saying was that I have 2 types of bookmarks that refer to page#s:
            – I might say “as discussed above on page x” where x is inserted as a x-ref to a bookmark on page x where I discussed whatever
            – For a quotation from some reference that spans 2 pages, I have bookmarked the entire quotation. The bookmark is then included in the {TA field…}. That way, when a TOA is done, the entry for the authority is provided as “Authority title…x-y” where x-y are the start/end page numbers of the bookmark. Of course, the ref to the bookmark is in hidden text.

            Neither of the above uses of x-ref’g a bookmark to a page# shows the Referenced-by info for the bookmark.

            Fred

      • #953125

        Andrew

        I have been looking for something like this for ages. Thank you.

        I have multi page (up to 100) documents with bookmarks throughout and have created TOC so your macro gives me lots and lots of information. We also have some documents supplied by our software developer (CRM) that have bookmarks that we need to know what they are. I run Word with the Tools|Options|Views Bookmarks checked all the time but this does not show the names.

        Again thank you

      • #989485

        This work well. Could you help me to sort those bookmarks so they come out in location order. Thanks.

        • #989487

          Here is a different and shortened version of Andrew’s macro. It produces a table in a new document listing bookmarks in the order they appear in the document. You can adapt it to your needs.

          Sub GetBookmarkInfo()
          Dim bk As Bookmark
          Dim colBkData As Collection
          Dim col As Collection
          Dim colRefs As Collection
          Dim doc As Document
          Dim strOutput As String
          Dim k As Long
          Dim docReport As Document

          Set colBkData = New Collection
          Set doc = ActiveDocument
          doc.Bookmarks.ShowHidden = True

          For Each bk In doc.Bookmarks
          Set col = New Collection
          col.Add Key:=”name”, Item:=bk.Name
          col.Add Key:=”start”, Item:=bk.Start
          colBkData.Add Item:=col
          Set col = Nothing
          Next bk
          Set docReport = Documents.Add
          docReport.Tables.Add _
          Range:=Selection.Range, _
          NumRows:=1, NumColumns:=2
          Selection.TypeText Text:=”Bookmark name”
          Selection.MoveRight Unit:=wdCell
          Selection.TypeText Text:=”Starting position”
          For k = 1 To colBkData.Count
          Selection.MoveRight Unit:=wdCell
          Selection.TypeText Text:=colBkData(k)(“name”)
          Selection.MoveRight Unit:=wdCell
          Selection.TypeText Text:=colBkData(k)(“start”)
          Next k

          docReport.Tables(1).Sort ExcludeHeader:=True, _
          FieldNumber:=2, SortFieldType:=wdSortFieldNumeric
          End Sub

          • #989542

            Thanks Hans, do you think it would be possible to look through several unopened documents and pull the bookmarks out into a new document?

            • #989543

              Would you like to collect information on all Word documents in a folder, or would you like the user to be able to select specific documents?

            • #989545

              Within a specfied folder.

            • #989550

              Try the attached code.

            • #989563

              Works good. I’m trying to get the bookmark text out of each bookmark in the documents. I’ve been trying to merge you previous attachment with something i’ve been working on that was giving me the output of the active document only. I’ve managed to get that with the attached but it seems to pulling the bookmarks multiple times from each document in the selected folder. Could you please see if you can figure where my problem is. Except for this is perfect.

            • #989567

              You don’t reset strOutput to an empty string for each document, so you keep on appending text to the string. Insert a line

              strOutput = “”

              immediately below the line

              Do While Not strFile = “”

            • #989571

              Thanks Much Hans, work great.

            • #989635

              I like the way this works. Having trouble modifying it to count shapes (checkbox shapes). I can get the total shapes, i’m trying to count the unchecked, checked and total number of shapes in a folder, then perform some calculations on the totals. I can’t seem to be able to count the checked and unchecked. It would really be helpful to count the shapes with checked/unchecked values with a certain value in its name. Any help would be appreciated.

    • #989633

      Fred hi

      I am not sure, without reading the whole thread, whether your question has been answered.

      I had a similar request sometime ago and found two suggestions, The first listed all the bookmarks at the beginning of the document and the second identified the location by showing the bookmark name. I have combined these two to list the bookmarks at the start of the document and show the locations by printing each bookmark in red italics at their location. Works great for me as it means you can check the names and locations real easy.

      Hope this helps.

      ps I can’t remember who developed the originals but whoever, Thanks

      edited by PhilC: Sorry forgot to include code!!!!!

      Sub PrintAndListAllBookmarks()
      Dim objBookmark As Bookmark
      Dim j As Integer

      For Each objBookmark In ActiveDocument.Bookmarks
      objBookmark.Select
      With Selection.Font
      .Italic = True
      .ColorIndex = wdRed
      End With
      Selection.Font.Size = 8
      Selection.TypeText Text:=objBookmark.Name
      Next

      Selection.HomeKey Unit:=wdStory
      Selection.TypeParagraph
      Selection.InsertBreak Type:=wdColumnBreak
      Selection.TypeText Text:=”Bookmark list for ”
      Selection.TypeText Text:=ActiveDocument.Name
      Selection.TypeParagraph
      For j = 1 To ActiveDocument.Bookmarks.Count
      Selection.TypeText Text:=Chr(9)
      Selection.TypeText Text:=ActiveDocument.Bookmarks(j).Name
      Selection.TypeParagraph
      Next j
      Selection.InsertBreak Type:=wdColumnBreak

      End Sub

      • #989642

        Hi Phil,

        Thanks for taking the time out to respond on this. It was about 6 months ago and Andrew Savitkis responded almost immediately and gave me something useful. Within the past few days, some other people resurrected the thread and Hans posted another macro with some variations to Andrew’s approach. I think those are the only 2 other postings with solutions, so if you wanted to compare theirs to yours, it shouldn’t take much. From your description, it seems like your macro may yet have another twist or two. So, I’ve saved your macro too and will check them all out if the need arises in the future. Thanks again.

        Fred

    Viewing 1 reply thread
    Reply To: Bookmark rpt (2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: