canesbr
asked on
Word Field needed to count number of words in a given bookmark
There is a functionality Tools>Word Count that will give you a result for a selection.
There is a field {NUMWORDS} that will give you a result for the document
How to modify the field so that you get the number of words for a bookmark, say SUMMARY?
Regards
Brian
There is a field {NUMWORDS} that will give you a result for the document
How to modify the field so that you get the number of words for a bookmark, say SUMMARY?
Regards
Brian
ASKER
Thanks
So there's no \bookmark qualifier or somesuch option within fields?
So how would one set up a MACROBUTTON field so that the lWordCount result shows up in the document; and is there a way to pass the bookmark name from the macrobutton field to to macro?
Regards
Brian
So there's no \bookmark qualifier or somesuch option within fields?
So how would one set up a MACROBUTTON field so that the lWordCount result shows up in the document; and is there a way to pass the bookmark name from the macrobutton field to to macro?
Regards
Brian
Some fields use bookmarks but most don't have any use for a bookmark. Sometimes a bookmark is optional in which case it is preceded by an option switch.
A Macrobutton is one way of launching a macro, but such a macro cannot take parameters. It does not need a bookmark, but may contain or be contained in one or more bookmarks. The macro cannot automatically discover the range of the macro button that launched it.
You could have bookmarked areas whose words were to be counted each associated with a bookmarked location to display the count.
A Macrobutton is one way of launching a macro, but such a macro cannot take parameters. It does not need a bookmark, but may contain or be contained in one or more bookmarks. The macro cannot automatically discover the range of the macro button that launched it.
You could have bookmarked areas whose words were to be counted each associated with a bookmarked location to display the count.
ASKER
Thanks
Okay
So would some macro like the following work?
"For each bookmark identifying a bookmark range (name ends in _WC) insert the word count for the range needed"?
So in the document one would have
Summary Word Count: 1111
Conclusion Word Count: 222
etc
Then bookmark "Summary Word Count: 1111" as Summary_WC etc
Then the loop can use the the portion of the name preceding _WC as the range to be counted.
Then the macro can replace the bookmark contents with the result?
Regards
Brian
Okay
So would some macro like the following work?
"For each bookmark identifying a bookmark range (name ends in _WC) insert the word count for the range needed"?
So in the document one would have
Summary Word Count: 1111
Conclusion Word Count: 222
etc
Then bookmark "Summary Word Count: 1111" as Summary_WC etc
Then the loop can use the the portion of the name preceding _WC as the range to be counted.
Then the macro can replace the bookmark contents with the result?
Regards
Brian
Yes. It can be a bit tricky. There is no collection by partial name, so you have to walk through all the bookmarks and test the name.
It is best to readd a containing bookmark after updating to ensure that it does contain the new text. This disturbs the collection, so that it Next bookmark is the one just added. There is therefore a test to skip over that one.
It is best to readd a containing bookmark after updating to ensure that it does contain the new text. This disturbs the collection, so that it Next bookmark is the one just added. There is therefore a test to skip over that one.
Dim bmk As Bookmark
Dim rng As Range
Dim strBookmarkName
For Each bmk In ActiveDocument.Bookmarks
If UCase$(Right$(bmk.Name, 3)) = "_WC" Then
If bmk.Name <> strBookmarkName Then 'not the one just added
strBookmarkName = bmk.Name
Set rng = bmk.Range
rng.Text = ActiveDocument.Bookmarks(Left$(bmk.Name, Len(bmk.Name) - 3)).Range.words.Count
ActiveDocument.Bookmarks.Add bmk.Name, rng 'redefine the bookmark
End If
End If
Next bmk
ASKER
Beautiful
So I think line 10 should be
ActiveDocument.Bookmarks.A dd strBookmarkName, rng 'redefine the bookmark
because at that point bmk.name has been deleted.
I guess now there should be checks that if there is a bookmark bleep_WC that bookmark bleep exists and that bleep_WC does not intersect with any other bookmark.
How would one do these two things?
Then I think we are done.
Thanks
Regards
Brian
So I think line 10 should be
ActiveDocument.Bookmarks.A
because at that point bmk.name has been deleted.
I guess now there should be checks that if there is a bookmark bleep_WC that bookmark bleep exists and that bleep_WC does not intersect with any other bookmark.
How would one do these two things?
Then I think we are done.
Thanks
Regards
Brian
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That does it
Thanks
Regards
Brian
Thanks
Regards
Brian
ASKER
I see I should have asked a related question for more points as opposed to asking within the question
Sorry
Regards
Brian
Sorry
Regards
Brian
You could find it like this
lWordCount = ActiveDocument.Bookmarks("