webwisewords
Oct 5th, 2005, 04:52 AM
I'm a freelance writer who does a lot of website content and, as such, I need to concentrate on keyword density quite often. Now, I know there are plenty of resources that will do this for me but I like to do things myself so I thought I'd create a little VBA to complete the task in Word. The following code asks for keyword and counts how many times that keyword appears in the text and then calculates the density but I have a couple of problems. I'll list the problems first then the code:
1 - It all works fine and dandy so long as the keyword is a single word. If I'm using a keyphrase that contains more than one word it still only calculates it as though there is one word. e.g. if keyword is 'keyword' and appears 4 times in 100 words it gives 4% which is fine, but change that to 'key word' and it still gives a density of 4%. By my reckoning that means that if you were to write 'key word' fifty times you would have 100 words but only a kd of 50%! I need to be able to work out the number of words in the given text but I don't know how to do this.
2 - Is there any way of excluding excluded words from the keywordcount count. That is, if the keyphrase is 'socks and underpants' it shouldn't include the word 'and'.
3 - Why is keyword density so flaming difficult for everyone to agree on?
And now, the monster code:
Sub CountWordPhrase()
Dim x, Response, ExitResponse
Dim KeyWord As Integer
Dim WordCount As Integer
WordCount = ActiveDocument.BuiltInDocumentProperties(wdPropertyWords)
On Error Resume Next
AskAgain:
x = InputBox("Please enter the keyword you wish to count and click OK." _
& Chr$(13) & Chr$(13) & _
"NOTE: This macro will find a whole word only. If the text you typed " _
& "is part of a larger string, it will also be found.")
If x = "" Or x = " " Then
ExitResponse = MsgBox("You either clicked Cancel or you did " & _
"not type a word. Do you want to quit?", vbYesNo)
If ExitResponse = 6 Then
End
Else
GoTo AskAgain
End If
Else
With ActiveDocument.Content.Find
Do While .Execute(FindText:=x, Forward:=True, Format:=True, _
MatchWholeWord:=True) = True
StatusBar = "Word is counting the occurrences of the text " & _
Chr$(34) & x & Chr$(34) & "."
KeyWord = KeyWord + 1
Loop
End With
Density = Int(KeyWord / WordCount * 100)
Response = MsgBox("The text " & Chr$(34) & x & Chr$(34) & " was found" _
& Str$(KeyWord) & " times. This is equivalent to " & Density & "%", vbOKOnly)
End If
End Sub
Thank you very much indeed.
1 - It all works fine and dandy so long as the keyword is a single word. If I'm using a keyphrase that contains more than one word it still only calculates it as though there is one word. e.g. if keyword is 'keyword' and appears 4 times in 100 words it gives 4% which is fine, but change that to 'key word' and it still gives a density of 4%. By my reckoning that means that if you were to write 'key word' fifty times you would have 100 words but only a kd of 50%! I need to be able to work out the number of words in the given text but I don't know how to do this.
2 - Is there any way of excluding excluded words from the keywordcount count. That is, if the keyphrase is 'socks and underpants' it shouldn't include the word 'and'.
3 - Why is keyword density so flaming difficult for everyone to agree on?
And now, the monster code:
Sub CountWordPhrase()
Dim x, Response, ExitResponse
Dim KeyWord As Integer
Dim WordCount As Integer
WordCount = ActiveDocument.BuiltInDocumentProperties(wdPropertyWords)
On Error Resume Next
AskAgain:
x = InputBox("Please enter the keyword you wish to count and click OK." _
& Chr$(13) & Chr$(13) & _
"NOTE: This macro will find a whole word only. If the text you typed " _
& "is part of a larger string, it will also be found.")
If x = "" Or x = " " Then
ExitResponse = MsgBox("You either clicked Cancel or you did " & _
"not type a word. Do you want to quit?", vbYesNo)
If ExitResponse = 6 Then
End
Else
GoTo AskAgain
End If
Else
With ActiveDocument.Content.Find
Do While .Execute(FindText:=x, Forward:=True, Format:=True, _
MatchWholeWord:=True) = True
StatusBar = "Word is counting the occurrences of the text " & _
Chr$(34) & x & Chr$(34) & "."
KeyWord = KeyWord + 1
Loop
End With
Density = Int(KeyWord / WordCount * 100)
Response = MsgBox("The text " & Chr$(34) & x & Chr$(34) & " was found" _
& Str$(KeyWord) & " times. This is equivalent to " & Density & "%", vbOKOnly)
End If
End Sub
Thank you very much indeed.