Solved

Word Field needed to count number of words in a given bookmark

Posted on 2008-06-13
9
369 Views
Last Modified: 2008-06-13
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
0
Comment
Question by:canesbr
  • 5
  • 4
9 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21780343
There isn't such a field. It would have to be handled with VBA.

You could find it like this

lWordCount = ActiveDocument.Bookmarks("SUMMARY").Range.Words.Count
0
 

Author Comment

by:canesbr
ID: 21780706
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
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21781187
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.
0
 

Author Comment

by:canesbr
ID: 21781326
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21782050
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.
    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

Open in new window

0
 

Author Comment

by:canesbr
ID: 21782616
Beautiful
So I think line 10 should be
                ActiveDocument.Bookmarks.Add 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
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 21783233
Yes it would be better to use the name from the string.

Bookmarks is one of the few collections in Word with an Exists method:

If ActiveDocument.Bookmarks.Exists("MyBookmark") Then

There is an InRange method, but that tests for one range being completely contained in another.

The snippet will test to see if two bookmarks overlap.




Function BookmarksOverlap(bmk1 As Bookmark, bmk2 As Bookmark) As Boolean

    Dim rng1 As Range

    Dim rng2 As Range

    Set rng1 = bmk1.Range

    Set rng2 = bmk2.Range

    If rng1.Start < rng2.Start Then

        If rng1.End > rng2.Start Then

            BookmarksOverlap = True

        End If

    Else

        If rng1.Start < rng2.End Then

            BookmarksOverlap = True

        End If

    End If

End Function

Open in new window

0
 

Author Comment

by:canesbr
ID: 21783286
That does it
Thanks
Regards
Brian
0
 

Author Comment

by:canesbr
ID: 21783301
I see I should have asked a related question for more points as opposed to asking within the question
Sorry
Regards
Brian
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Like many others, we try and discourage users from printing documents unnecessarily and instead send or share them electronically. However, this doesn't always work and documents are still printed. With this simple solution, if the user tries to …
I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now