1
votes

I'm trying to make the "number of occurrences" either be written in red or in bolded red. Can someone please point me in the right direction. I'm new to coding. This is a word-counter, and when 2+ words are found...it displays the number of words found at the bottom of the word document.

Sub a3()

Dim Word As String
Dim wcount As Integer

Word = InputBox("Search for a word")
If (Word <= "") Then
    MsgBox ("Did not enter word")
End If

If (Word > "") Then
    wcount = 0
    With Selection
            .HomeKey Unit:=wdStory
            With ActiveDocument.Content.Find
                .Text = Word
                Do While .Execute
                    wcount = wcount + 1
                    Selection.MoveRight
                Loop
            End With
            MsgBox ("The word: '" & Word & "' shows up " & wcount & " times in the document")
        End With
    End If

If (wcount <= 2) Then
  ActiveDocument.Content.InsertAfter Text:=(vbCrLf & "Number occurrences: " & wcount)
  Selection.Font.ColorIndex = wdRed

ElseIf (wcount <= 3) Then
    ActiveDocument.Content.InsertAfter Text:=(vbCrLf & "Number occurrences: " & wcount)
    Selection.Font.ColorIndex = wdRed
    Selection.Font.Bold = True

Else
    ActiveDocument.Content.InsertAfter Text:=(vbCrLf & "Number occurrences: " & wcount)
    Selection.Font.ColorIndex = wdBlack
    Selection.Font.Bold = False

End If

    End Sub
2

2 Answers

0
votes

Working with Word Range objects will help with this. Think of a Range like an invisible selection, except that code can work with multiple Range objects, while there can be only one Selection.

Assign the document's content to a Range, then perform the Find and extension on that. Then the formatting can also be applied to the Range. I've altered (but not tested) the code in the question to demonstrate.

In the last part, where text is written at the end of the document, the Range object is set to the entire document, then collapsed (think of it like pressing the right-arrow key with a selection). Then the new text is assigned to the range and formatting applied. Because the range will contain only the new text, the formatting is applied to that, only.

(Additional notes: I've changed the Word variable name to sWord because "Word" could be misunderstood to mean the Word application. I've also changed the comparison to check whether sWord contains something to Len(sWord) > 0 because the "greater than """ comparison is not guaranteed.)

Sub a3()

Dim sWord As String
Dim wcount As Integer
Dim rng as Word.Range

Set rng = ActiveDocument.Content
sWord = InputBox("Search for a word")
If (sWord <= "") Then
    MsgBox ("Did not enter word")
End If

If (Len(sWord) > 0) Then
    wcount = 0
    With Selection
            .HomeKey Unit:=wdStory
            With rng.Find
                .Text = sWord
                Do While .Execute
                    wcount = wcount + 1
                    rng.Collapse wdCollapseEnd
                Loop
            End With
            MsgBox ("The word: '" & sWord & "' shows up " & wcount & " times in the document")
        End With
    End If

Set rng = ActiveDocument.Content
rng.Collapse wdCollapseEnd
If (wcount <= 2) Then
  rng.Text = (vbCrLf & "Number occurrences: " & wcount)
  rng.Font.ColorIndex = wdRed

ElseIf (wcount <= 3) Then
    rng.Text = (vbCrLf & "Number occurrences: " & wcount)
    rng.Font.ColorIndex = wdRed
    rng.Font.Bold = True
Else
    rng.Text = (vbCrLf & "Number occurrences: " & wcount)
    rng.Font.ColorIndex = wdBlack
    rng.Font.Bold = False
End If

    End Sub
0
votes

There are many ways to do this, some of them are based on a preference for ranges or selections and also the structure of the Find statement. Here is my preference.

Sub a3()
    Dim wrd As String
    Dim wcount As Integer
    Dim rng As Word.Range
    wrd = InputBox("Search for a word")
    If wrd = vbNullString Then
        MsgBox ("Did not enter word")
        Exit Sub
    End If
    Set rng = ActiveDocument.Content
    wcount = 0
    With rng.Find
        .ClearFormatting
        .Format = False
        .Forward = True
        .MatchWholeWord = True
        .Text = wrd
        .Wrap = wdFindStop
        .Execute
        Do While .found
            wcount = wcount + 1
            rng.Collapse Word.WdCollapseDirection.wdCollapseEnd
            .Execute
        Loop
    End With
    MsgBox ("The word: " & "" & wrd & "" & " shows up " & wcount & " times in the document")

    ActiveDocument.Content.InsertParagraphAfter
    Set rng = ActiveDocument.Content
    rng.Collapse Word.WdCollapseDirection.wdCollapseEnd
    rng.Text = "Number occurrences: " & wcount
    If wcount < 3 Then
        rng.Font.ColorIndex = wdRed
    ElseIf wcount < 4 Then
        rng.Font.ColorIndex = wdRed
        rng.Font.Bold = True
    Else
        rng.Font.ColorIndex = wdAuto
        rng.Font.Bold = False
    End If
End Sub