• Macro to word-count bookmarks (Word 2000 SR1)

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Macro to word-count bookmarks (Word 2000 SR1)

    Author
    Topic
    #380521

    Hi all. I write for a living and rely extensively on word counts – so much so that I have created a toolbar button that invokes the word count on a selection of text (“so what?” you all yawn). But it’s time for more counting horsepower.

    Though I’m a fairly skilled Word user, coding macros is out of my comfort zone, so… can anyone help me create a macro that will do what I need?

    I’d like it to operate on a defined selection (probably a manually created bookmark for simplicity), and display the result in the location of my choosing (probably immediately after the selection, or in a summary section at the beginning or end of the document). I’m not sure how this would be implemented – I was thinking that maybe the macro could create a new bookmark with a name derived from the source bookmark (e.g. “Chapter_3_Word_Count”), and simply stick the count value in there.

    Counter updates don’t need to be dynamic. If I could achieve the above level of functionality, I think I’d be happy initiating an update macro manually whenever I needed to get the up-to-date counts for all bookmarks.

    But these are just ideas from an enthusiastic amateur – if anyone out there has some ready-made code or even better ideas, I’d be very grateful to hear from you.

    Thanks heaps

    Neil

    Viewing 0 reply threads
    Author
    Replies
    • #637711

      Neil,

      Here’s something that approximates what you describe:

      Public Sub WordCountBookmarks()
      
      'Gary Frieder 2002
      Dim strBmkName As String
      Dim strInsertText As String
      Dim lngWordCt As Long
      Dim rngTemp As Range
      
      If Selection.Bookmarks.Count = 0 Then Exit Sub
      
      Application.ScreenUpdating = False
      
      strBmkName = Selection.Bookmarks.Item(1).Name
      
      ActiveDocument.Bookmarks(strBmkName).Range.Select
      
      With Dialogs(wdDialogToolsWordCount)
         lngWordCt = .Words
         .Execute
      End With
      
      strInsertText = "Bookmark " & strBmkName & " contains " & CStr(lngWordCt) & " words."
      
      Selection.EndKey Unit:=wdStory
      Selection.InsertParagraphAfter
      Selection.TypeText strInsertText
      Set rngTemp = Selection.Paragraphs(1).Range
      ActiveDocument.Bookmarks.Add (strBmkName & "WordCount"), rngTemp
      
      Application.ScreenUpdating = True
      
      Set rngTemp = Nothing
      
      End Sub
      

      Gary

      • #637769

        Gary

        Wow – thanks! This is awesome… I’m truly impressed.

        As far as I can tell, what I have to do is select the contents of a bookmark, then run the macro and it appends the word count at the end of the document within a bookmark.

        At the risk of stretching a friendship, can you suggest if it would be possible for the macro to list the available bookmarks, so that one can be selected by name, then automatically execute the counter on the appropriate one? Also – if I run it repeatedly on one section, it seems to recreate the ‘report’ each time, redefining the bookmark with the new report text, and leaving the old one behind (i.e. so there is a series of them for the same bookmark). If so, would it be a big deal to simply delete the previous one (if exists, i.e. it’s not the first run) prior to writing the new one?

        Gary – once again, many thanks for your help with this… it’s a brilliant start!

        Very best regards

        Neil

        • #637777

          I would recommend against putting the information into the file. You can never rely on the information being correct unless you are sure the macro was run and no words were added after that. I would recommend another tack where you run the macro to get the current counts on all bookmarks. This code would look like this

          Public Sub WordCountBookmarks()
            Dim strBmkName As String
            Dim sText As String
            Dim lngWordCt As Long
            Dim rngTemp As Range, abkmk As Bookmark
            sText = "Word Counts are" & vbCr & vbCr
            Application.ScreenUpdating = False
            For Each abkmk In ActiveDocument.Bookmarks
              abkmk.Select
              With Dialogs(wdDialogToolsWordCount)
                lngWordCt = .Words
                .Execute
              End With
              sText = sText & lngWordCt & vbTab & abkmk.Name & vbCr
            Next abkmk
            Application.ScreenUpdating = True
            MsgBox sText, vbOKOnly, "Bookmark Word Counts"
          End Sub

          This method will display the counts for all bookmarks as a message box.

        • #637977

          I posted a macro earlier this year that populated a new toolbar with a dropdown listing your bookmarks. When you chose one from the list, a macro ran. In that case, the macro that ran selected the bookmark, but you could use it to run Gary’s macro. Let me know if you have trouble finding the thread, it’s around here somewhere…

    Viewing 0 reply threads
    Reply To: Macro to word-count bookmarks (Word 2000 SR1)

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

    Your information: