** Resolved ** Word 2000 VBA Word Count Question
When I prepare my weekly sermon, I usually manuscipt my notes and use them as a general guide for what I intend to speak about. Helps me keep focused and on time. Usually when I step into the pulpit, after announcing the message title, I usually announce the key word or phrase to watch for and count during the course of the message. This is primarily for the children and youth but I've found that many of the adults come up after church and say something like, "Pastor, I think you used the word 'justified' about 19 times in your message."
What I'd like to be able to do is write some VBA code that when executed would popup a message box that would prompt me to enter a key word of phrase to be counted and upon entering it and pressing ok, it would proceed to count the number of instances the key word or phrase is used in the document.
I would like to be able, if possible to launch this VBA code from a button on a toolbar if possible. I am assuming this code would need to be in the Normal.dot templete so that the VBA code code be used with any document I create.
I've done a little VB6 prgramming of database applications for my church and office but no real VBA programming in Word.
Any help with examples would be greaty apprciated.
Thanks,
Pastor Mike
Re: Word 2000 VBA Word Count Question
This will run a search for a word or phrase that you get prompted for and display a message box with the total.
You can easily modify it for case sensitivity, like words, etc.
VB Code:
Private Sub CountMe()
Dim sWord As String
Dim iCount As Integer
sWord = InputBox("Enter the 'key word' or 'phrase' to count.", "Key word or Phrase", vbNullString)
If Len(sWord) = 0 Then Exit Sub
iCount = 0
ActiveDocument.Select
With Selection.Find
.ClearFormatting
.Style = wdStyleBodyText
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Wrap = wdFindContinue
Do While .Execute(FindText:=sWord, Forward:=True, Wrap:=False, Format:=False) = True
If .Found = True Then
iCount = iCount + 1
End If
Loop
MsgBox iCount & " instances of '" & sWord & "'!", vbOKOnly + vbInformation
End With
End Sub
:D
Re: Word 2000 VBA Word Count Question
Thanks Rob. It wrks great. Another stupid question, how would I run this from button on the Word toolbar?
Mchael
1 Attachment(s)
Re: Word 2000 VBA Word Count Question
Change CountMe so its Public instead of Private. Paste it into the Normal VBA IDE window.
Then create a new toolbar: View > ToolBars > Customize....
In the Toolbars tab click "New..." and name your toolbar. Select to make it available to Normal.dot. Now your
balnk toolbar will be showing.
To assign the CountMe macro to a new button...
Commands tab > select Macros > select "Normal.ThisDocument.CountMe"
Drag the selection to your new toolbar and drop it there.
You can now customize it.
Click the "Modify Selection" button which now becomes enabled. In the Name menu item click in there and rename
the "Normal.ThisDocument.CountMe" to "CountMe". You toolbar button will change caption to "CountMe".
You can assign an image to the button also...
Click the "Modify Selection" button again and select the "Change Button Image" menu. Then select an image from the list.
Now you can click ok and close it. You can then drag your toolbar to the main toolbar docking area and
arrange it to suit your needs.
You can also assign the macro to a keyboard shortcut ;)
Enjoy :)
Re: Word 2000 VBA Word Count Question
Thanks Rob. I hate to be a pest but apparently I am doing something wrong or missing something. The macr works fine. However, I am having a problem when attempting to place it on a toolbar.
You said, "In the Toolbars tab click "New..." and name your toolbar. Select to make it available to Normal.dot. Now your blank toolbar will be showing." When I do this, the toolbar immediately appears in the middle of the page and cannnot be moved.
As a work around, I placed a button on an existing toolbar (the standard wordcount bar) and attempted to change the name as you suggested (Click the "Modify Selection" button which now becomes enabled. In the Name menu item click in there and rename the "Normal.ThisDocument.CountMe" to "CountMe". You toolbar button will change caption to "CountMe".)
However, the "Modify Selection" is not visable at all when Macros is selected.
Other than that it works great.
Michael
Re: Word 2000 VBA Word Count Question
When adding the macro to your custom toolbar make sure the toolbar is selected in the toolbar tab before
proceeding. Then you can drag a macro to it to create a new button. The Modify Selection will become visible/enabled.
The toolbar should always become visible right after creation and be moveable.
Did you paste the macro code into the Normal.dot ThisDocument class? and change it from Private to Public?
When you select the Macros item, does the CountMe macro list in the little window on the right of it? If it doesnt
then its either still Private or its not in the Normal.Dot template. This could be why the Modify Selection button is not visible.
Re: Word 2000 VBA Word Count Question
Thanks Rob. It seems to be working now. The toolbar became visable immediately but was not movable until I place a button on it. After that everything worked ok.
Michael
Re: ** Resolved ** Word 2000 VBA Word Count Question
Cool! :thumb:
Just wanted to mention that the code may need to be tweeked a bit if you need to count other areas like
Headers/Footers/Headings, etc.
The line - ".Style = wdStyleBodyText" sets the Find to look only at the body text. There are other setting like I mentioned
but if your just using a basic document layout then you should be ok.
:afrog: