Count words in word vba

title keywords f1_keywords ms.prod api_name ms.assetid ms.date ms.localizationpriority

Words.Count property (Word)

vbawd10.chm157024258

vbawd10.chm157024258

word

Word.Words.Count

abbb4293-0ffb-f845-cdda-acbbe0ff477b

06/08/2017

medium

Words.Count property (Word)

Returns a Long that represents the number of words in the collection. Read-only.

Syntax

expression.Count

expression Required. A variable that represents a ‘Words’ collection.

Example

This example displays the number of words in the selection.

If Selection.Words.Count >= 1 And _ 
 Selection.Type <> wdSelectionIP Then 
 MsgBox "The selection contains " & Selection.Words.Count _ 
 & " words." 
End If

See also

Words Collection Object

[!includeSupport and feedback]

I am trying to implement a programme that counts the words in a multiline textbox as you type. I can get it counting the words until I press the «enter» key and type a word. It does not recognise this. This is my code:

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles TextBox1.TextChanged
  Dim str As String
  Dim i, l, words As Integer
  str = TextBox1.Text

  str = LTrim(str) 'removes blank spaces at the beginning of text
  str = RTrim(str) ' removes blank spaces at the end of text
  l = str.Length
  i = 0
  words = 0
  While (i < l)
    If str(i) = " " Then
      words = words + 1
      i = i + 1
      While str(i) = " "  ' removes more than 1  blank space
        i = i + 1
      End While
    Else
      i = i + 1
    End If

  End While

  words = words + 1 ' adds the last word

  TextBox2.Text = ("" & words)

End Sub 

Aon Novas's user avatar

asked Feb 3, 2012 at 0:04

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    Static rex As New System.Text.RegularExpressions.Regex("b", System.Text.RegularExpressions.RegexOptions.Compiled Or System.Text.RegularExpressions.RegexOptions.Multiline)

    Label1.Text = (rex.Matches(TextBox1.Text).Count / 2).ToString()

End Sub

answered Feb 3, 2012 at 1:28

GSerg's user avatar

GSergGSerg

75.3k17 gold badges160 silver badges340 bronze badges

2

Here’s another regex solution:

Dim WordCount = New Regex("w+").Matches(s).Count

answered Feb 3, 2012 at 1:45

ron tornambe's user avatar

ron tornamberon tornambe

10.3k7 gold badges32 silver badges59 bronze badges

Why not change to regex that whole thing can be like this

Dim words As MatchCollection = Regex.Matches(value, "s+")
words.Count

answered Feb 3, 2012 at 1:31

Raymund's user avatar

RaymundRaymund

7,5595 gold badges46 silver badges78 bronze badges

2

You need to remove the «return» key

Add these lines:

str = Replace(str, "chr(10)", "")  'Replaces  line feed
str = Replace(str, "chr(13)", "")  'Replaces carriage return

before

  str = LTrim(str) 'removes blank spaces at the beginning of text
  str = RTrim(str) ' removes blank spaces at the end of text 

answered Feb 3, 2012 at 1:35

jordanhill123's user avatar

jordanhill123jordanhill123

4,1222 gold badges30 silver badges40 bronze badges

this is my way of counting words

    Dim count As Integer = 0
    For Each word As String In Split(txtOutput.Text, " ")
        count += 1
    Next
    MsgBox("i counted " + count.ToString + " words")

answered Jul 7, 2015 at 0:18

manolo's user avatar

1

Don’t use

str = Replace(str, "chr(10)", "")  'Replaces  line feed
str = Replace(str, "chr(13)", "")  'Replaces carriage return

Better

str = Replace(str, vbCrLf, " ")

Alex's user avatar

Alex

21.1k10 gold badges62 silver badges72 bronze badges

answered Mar 17, 2016 at 18:42

Luc's user avatar

If you would like an easy word count without using Regex

Dim s As String = " Count    the   words "

Dim iWords As Integer = 0
Dim bNonPrintable As Integer = 1

Dim len As Integer = s.Length - 1
Dim i As Integer

For i = 0 To len
    If s(i) = " "c Or s(i) = Chr(10) Or s(i) = Chr(13) Then
        If bNonPrintable = 0 Then
            iWords += 1

            bNonPrintable = i
        End If
    Else
        If bNonPrintable > 0 Then bNonPrintable = 0
    End If
Next
If bNonPrintable = 0 Then iWords += 1

iWords value will be 3 in this case (no need to use trim or replaces)

answered Oct 24, 2017 at 9:21

Ricardo González's user avatar

Public Function NumWords(Txt As String) As Long
    Dim i As Long
    If Len(Trim(Txt)) = 0 Then
        NumWords = 0
    Else
        Txt = Replace(Txt, vbNewLine, " ")
        For i = 255 To 2 Step -1
            Txt = Replace(Txt, Space(i), " ")
        Next i
        NumWords = Len(Trim(Txt)) - Len(Replace(Txt, " ", "")) + 1
    End If
End Function

answered Nov 3, 2019 at 10:50

Junjie Fallorina's user avatar

1

I have seen a lot of examples where people count empty spaces and think these are equivalent to the count of words. Well this is not correct and you should not use this method. There are several cases I can think of where this does not work:

  1. When an empty space is not followed by a word, the number of spaces is +1 the number of words.
  2. You can have double spaces, they will count as +1 word as well.
  3. Most importantly, you can have a word followed by a newline character(s) and then followed by another word. There are no spaces here, so the whole thing will be wrongfully considered one word instead of two.

Now here is what I have come up with. Add an empty space to the beginning of an existing string. Then replace all newline occurrences with empty space characters. Then check one by one the characters from the start of the string, if the current character is an empty space and character following it is not, then you got a word. Simple as that! Works with consecutive spaces, newline characters and pretty much anything I could throw at it.

Implementation in VB6:

Private Function countwords(cstring As String) As Long
cstring = " " & cstring
cstring = Replace(cstring, vbNewLine, " ")
countwords = 0
    For i = 1 To Len(cstring) - 1
        If Mid(cstring, i, 1) = " " And Mid(cstring, i + 1, 1) <> " " Then
            countwords = countwords + 1
        End If
    Next
End Function

answered Feb 11, 2021 at 21:52

Sh.K's user avatar

Sh.KSh.K

2793 silver badges4 bronze badges

  • Remove From My Forums
  • Question

  • Hi all! I want create a macro to count the black text in my word document. Could you please help me. Much obliged!

    • Edited by

      Tuesday, December 11, 2018 4:19 PM

Answers

  • No idea whether this will work, give it a try.

    Sub CountChinese()
        Dim w As Range
        Dim n As Long
        For Each w In ActiveDocument.Words
            Select Case w.LanguageIDFarEast
                Case wdTraditionalChinese, wdSimplifiedChinese
                    n = n + 1
            End Select
        Next w
        MsgBox «The number of Chinese words in the active document is » & n, vbInformation
    End Sub


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by
      Rainbow Walker
      Saturday, February 9, 2019 3:21 AM

Like this post? Please share to your friends:
  • Count words in word doc
  • Count words and characters in word
  • Count word occurrences in python
  • Count word in list python
  • Count with if condition in excel