From: TLee3 on 2 Nov 2009 11:34 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 ? -- Regards, TLee3
From: Pesach Shelnitz on 2 Nov 2009 13:16 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 -- Hope this helps, Pesach Shelnitz "TLee3" wrote: > 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 ? > -- > Regards, > TLee3
From: DaveLett on 2 Nov 2009 13:16 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
From: TLee3 on 5 Nov 2009 19:24 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 -- Regards, TLee3 "DaveLett" wrote: > 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
From: TLee3 on 5 Nov 2009 19:37 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 -- Regards, TLee3 "DaveLett" wrote: > 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
|
Pages: 1 Prev: how to unlock microsoft office 2007 Next: Run Word Macro from Addin |