Results 1 to 5 of 5
  1. #1
    Star Lounger
    Join Date
    Sep 2001
    Location
    Perth, Western Australia
    Posts
    89
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Macro to word-count bookmarks (Word 2000 SR1)

    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

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Macro to word-count bookmarks (Word 2000 SR1)

    Neil,

    Here's something that approximates what you describe:

    <pre>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
    </pre>

    Gary

  3. #3
    Star Lounger
    Join Date
    Sep 2001
    Location
    Perth, Western Australia
    Posts
    89
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macro to word-count bookmarks (Word 2000 SR1)

    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

  4. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Macro to word-count bookmarks (Word 2000 SR1)

    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
    <pre>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</pre>

    This method will display the counts for all bookmarks as a message box.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Macro to word-count bookmarks (Word 2000 SR1)

    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...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •