Word Count between Bookmarks

T

TLee3

Hello. I am trying to do a word count (via Word's built-in dialog box,
wdDialogToolsWordCount) between bookmarks. I know I must create a selection
between the bookmarks but I can't seem to do this properly. Can anyone help ?
 
P

Pesach Shelnitz

Hi,

I don't know how to do it with the wdDialogToolsWordCount dialog box, but
the following macro gives the word count between two bookmarks. Maybe it will
serve your purposes.

Sub WordCoundBetweenBookmarks()
Dim myRange As Range
Dim bkName1 As String
Dim bkName2 As String
Dim pos1 As Long
Dim pos2 As Long

With ActiveDocument
Set myRange = .Range
bkName1 = InputBox("Type the name of the first bookmark")
If .Bookmarks.Exists(bkName1) = False Then
MsgBox "The first bookmark specified does not exist."
GoTo ErrorHandler
Else
pos1 = .Bookmarks(bkName1).start
End If
bkName2 = InputBox("Type the name of the second bookmark")
If .Bookmarks.Exists(bkName2) = False Then
MsgBox "The second bookmark specified does not exist."
GoTo ErrorHandler
Else
pos2 = .Bookmarks(bkName2).End
End If
End With
If pos2 < pos1 Then
MsgBox "The second bookmark is located " & _
"before the first bookmark."
GoTo ErrorHandler
End If
myRange.start = pos1
myRange.End = pos2
MsgBox "Word count: " & myRange.Words.Count
ErrorHandler:
Set myRange = Nothing
End Sub
 
D

DaveLett

Hello,
I think you can use something like the following:

Dim orng As Range
Set orng = ActiveDocument.Bookmarks("Test1").Range
With orng
.Start = .End
.End = ActiveDocument.Bookmarks("Test2").Range.End
.Select
End With
Dialogs(wdDialogToolsWordCount).Display


HTH,
Dave
 
T

TLee3

Thanks Pesach & Dave - your code was very helpful as I evolved the following
solution.

Dim bkRange as Range


Set bkRange = Selection.Range
bkRange.Start = ActiveDocument.Bookmarks("NumBookMark" & bkNum).Range.Start
bkRange.End = ActiveDocument.Bookmarks("NumBookMark" & bkNum + 1).Range.End
'MsgBox bkRange.Start & vbTab & bkRange.End 'Show location of
bookmarks.
bkRange.Select

'Perform wordcount on selection
Set wordcount = Dialogs(wdDialogToolsWordCount)
wordcount.Execute
numWords = wordcount.Words
'Temporary message box.
MsgBox "The number of words in this selection = " & numWords, vbOKOnly,
"Word Count"

Selection.Collapse 'Un-selects text
 
T

TLee3

Thanks Pesach & Dave - your code was very helpful as I evolved the following
solution (tested) {prior response message was sent prematurely, please ignore}

Dim bkRange as Range
Dim wordcount '(not sure what to Dim as, left implicit)
Dim numWords as Integer

Set bkRange = Selection.Range
bkRange.Start = ActiveDocument.Bookmarks("FirstBookmark).Range.Start
bkRange.End = ActiveDocument.Bookmarks("SecondBookmark").Range.End
bkRange.Select 'The range must be selected
'Perform wordcount on selection
Set wordcount = Dialogs(wdDialogToolsWordCount)
wordcount.Execute 'execute the Word Count dialog box
numWords = wordcount.Words 'returns only words, saves variable for
later use

'Temporary message box.
MsgBox "The number of words in this selection = " & numWords, vbOKOnly,
"Word Count"

Selection.Collapse 'Un-selects text in range
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top