Results 1 to 12 of 12

Thread: Word and Character counter in VB6?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2011
    Location
    Canada
    Posts
    23

    Question Word and Character counter in VB6?

    I'm currently working on rich text editor, which I'm planning to add loads of functions into it. I'm just not sure on how to make a Word and Character counter for it. Not sure why I want it; but it just looks cool.

    Any help would be appreciated, and thanks in advance.

  2. #2
    Banned
    Join Date
    May 2011
    Posts
    27

    Re: Word and Character counter in VB6?

    Umm im gussing ur making like a program that counts words and spaces like teachers for typing classes have ur use thats actualy a simple program to make i already made one for my sisster inlaw for her when she was learning to type for work. Is this what ur looking to do?

  3. #3
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Word and Character counter in VB6?

    I made a quick example:
    vb Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.  
    5.     Dim tempArray()     As String
    6.     Dim lngWordCount    As Long
    7.     Dim lngCharCount    As Long
    8.     Dim lngCharCountS    As Long
    9.    
    10.     tempArray = Split(Trim$(RichTextBox1.Text), " ")
    11.    
    12.     lngWordCount = UBound(tempArray) + 1    '~~~ Number of words. ie, delimited by a single space
    13.     lngCharCount = Len(RichTextBox1.Text)   '~~~ Number of characters including spaces
    14.     lngCharCountS = Len(Replace(RichTextBox1.Text, " ", "")) '~~~ Number of characters (excludes white spaces)
    15.    
    16.     '~~~ Display it
    17.     MsgBox "Word Count = " & lngWordCount & vbNewLine & _
    18.             "Character count(includes white spaces) = " & lngCharCount & vbNewLine & _
    19.             "Character count(without white spaces) = " & lngCharCountS
    20.            
    21. End Sub
    You could additional things or alter it suit your needs.

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  4. #4
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,238

    Re: Word and Character counter in VB6?

    This is what I use on my improved NotePad app.

    Code:
    Private Sub mnuCharCount_Click()
    
    Dim Chars As Long
    Dim I As Long
    
    Chars = 0
    For I = 0 To Len(rtbText.Text) - 1
        rtbText.SelStart = I
        rtbText.SelLength = 1
        If Asc(rtbText.SelText) > 32 And Asc(rtbText.SelText) <= 126 Then
            Chars = Chars + 1
        End If
    Next
    MsgBox "The number of CHARACTERS is:" & vbCrLf & Chars, vbInformation, "Character Count"
    
    End Sub
    And a Word count.
    Code:
    Private Sub mnuWordCount_Click()
    
    Dim AsciiThisChar As Integer
    Dim AsciiLastChar As Integer
    Dim Words As Long
    Dim I As Long
    Dim LastChar As String * 1
    Dim ThisChar As String * 1
        
    rtbText.SelStart = 0
    rtbText.SelLength = 1
    LastChar = rtbText.SelText
        
    Words = 0
    For I = 1 To Len(rtbText.Text)
        rtbText.SelStart = I
        rtbText.SelLength = 1
        ThisChar = rtbText.SelText
        AsciiThisChar = Asc(ThisChar)
        AsciiLastChar = Asc(LastChar)
        If ((AsciiThisChar = 13 Or AsciiThisChar = 10) Or (AsciiThisChar >= 32 And AsciiThisChar <= 47) Or (AsciiThisChar >= 58 And AsciiThisChar <= 64) Or (AsciiThisChar >= 91 And AsciiThisChar <= 96) Or (AsciiThisChar >= 123 And AsciiThisChar <= 126)) And ((AsciiLastChar >= 48 And AsciiLastChar <= 57) Or (AsciiLastChar >= 65 And AsciiLastChar <= 90) Or (AsciiLastChar >= 97 And AsciiLastChar <= 122)) Then
            Words = Words + 1
        End If
        LastChar = ThisChar
    Next
        
    MsgBox "The number of WORDS is: " & vbCrLf & Words, vbInformation, "Number of Words"
    
    End Sub
    Keith

    I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

  5. #5
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,238

    Re: Word and Character counter in VB6?

    Yes yours is pretty quick akhileshbc.

    I did mine 13 years ago but I've never bothered with it since.
    Keith

    I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: Word and Character counter in VB6?

    Quote Originally Posted by akhileshbc View Post
    I made a quick example:
    vb Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.  
    5.     Dim tempArray()     As String
    6.     Dim lngWordCount    As Long
    7.     Dim lngCharCount    As Long
    8.     Dim lngCharCountS    As Long
    9.    
    10.     tempArray = Split(Trim$(RichTextBox1.Text), " ")
    11.    
    12.     lngWordCount = UBound(tempArray) + 1    '~~~ Number of words. ie, delimited by a single space
    13.     lngCharCount = Len(RichTextBox1.Text)   '~~~ Number of characters including spaces
    14.     lngCharCountS = Len(Replace(RichTextBox1.Text, " ", "")) '~~~ Number of characters (excludes white spaces)
    15.    
    16.     '~~~ Display it
    17.     MsgBox "Word Count = " & lngWordCount & vbNewLine & _
    18.             "Character count(includes white spaces) = " & lngCharCount & vbNewLine & _
    19.             "Character count(without white spaces) = " & lngCharCountS
    20.            
    21. End Sub
    You could additional things or alter it suit your needs.
    I would modify that slightly in case there are extra spaces like the two spaces after periods that some people use.
    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3.     Dim tempArray()     As String
    4.     Dim lngWordCount    As Long
    5.     Dim lngCharCount    As Long
    6.     Dim lngCharCountS    As Long
    7.     Dim strTemp1 As String
    8.     Dim strTemp2 As String
    9.    
    10.     strTemp1 = RichTextBox1.Text
    11.     Do Until LenB(strTemp1) = LenB(strTemp2)
    12.         strTemp2 = Replace(strTemp1, "  ", " ") ' replace 2 spaces with 1 space
    13.         strTemp1 = strTemp2
    14.     Loop
    15.    
    16.     tempArray = Split(Trim$(strTemp1), " ")
    17.    
    18.     lngWordCount = UBound(tempArray) + 1    '~~~ Number of words. ie, delimited by a single space
    19.     lngCharCount = Len(RichTextBox1.Text)   '~~~ Number of characters including spaces
    20.     lngCharCountS = Len(Replace(RichTextBox1.Text, " ", "")) '~~~ Number of characters (excludes white spaces)
    21.    
    22.     '~~~ Display it
    23.     MsgBox "Word Count = " & lngWordCount & vbNewLine & _
    24.             "Character count(includes white spaces) = " & lngCharCount & vbNewLine & _
    25.             "Character count(without white spaces) = " & lngCharCountS
    26.            
    27. End Sub

  7. #7
    Addicted Member
    Join Date
    Jul 2007
    Posts
    228

    Re: Word and Character counter in VB6?

    Here's a very fast method using the CopyMemory API:

    In the Declarations section put:

    Code:
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
        Destination As Any, _
        Source As Any, _
        ByVal Length As Long)
    In a Module add this function:

    Code:
    Public Function WordCount(Text As String) As Long
        Dim dest() As Byte
        Dim i As Long
    
        If LenB(Text) Then
            ' Move the string's byte array into dest ()
            ReDim dest(LenB(Text))
            CopyMemory dest(0), ByVal StrPtr(Text), LenB(Text) - 1
          
          ' Now loop through the array and count the words
            For i = 0 To UBound(dest) Step 2
    
                If dest(i) > 32 Then
    
                    Do Until dest(i) < 33
                        i = i + 2
                    Loop
                    WordCount = WordCount + 1
                End If
            Next i
            Erase dest
        Else
            WordCount = 0
        End If
    End Function
    Since Rich Text Editors have been done many times, you may benefit from downloading and viewing code on existing projects. The following editor project will not only generate Word Count but character count (different than word count) and line count... plus many other features good editors contain:

    http://www.planet-source-code.com/vb...69067&lngWId=1

    Good luck,

    Tom

  8. #8
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Word and Character counter in VB6?

    @Keithuk, @MartinLiss and @Tom Moran : Nice code


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  9. #9
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: Word and Character counter in VB6?

    Quote Originally Posted by akhileshbc View Post
    @Keithuk, @MartinLiss and @Tom Moran : Nice code

    Thanks, but my code was wrong. It only caught the first multiple space.
    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3.     Dim tempArray()     As String
    4.     Dim lngWordCount    As Long
    5.     Dim lngCharCount    As Long
    6.     Dim lngCharCountS    As Long
    7.     Dim strTemp As String
    8.     Dim intLen As Integer
    9.    
    10.     strTemp = RichTextBox1.Text
    11.     Do Until Len(strTemp) = intLen
    12.         intLen = Len(strTemp)
    13.         strTemp = Replace(strTemp, "  ", " ")
    14.     Loop
    15.     tempArray = Split(Trim$(strTemp), " ")
    16.    
    17.     lngWordCount = UBound(tempArray) + 1    '~~~ Number of words. ie, delimited by a single space
    18.     lngCharCount = Len(RichTextBox1.Text)   '~~~ Number of characters including spaces
    19.     lngCharCountS = Len(Replace(RichTextBox1.Text, " ", "")) '~~~ Number of characters (excludes white spaces)
    20.    
    21.     '~~~ Display it
    22.     MsgBox "Word Count = " & lngWordCount & vbNewLine & _
    23.             "Character count(includes white spaces) = " & lngCharCount & vbNewLine & _
    24.             "Character count(without white spaces) = " & lngCharCountS
    25.            
    26. End Sub

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Mar 2011
    Location
    Canada
    Posts
    23

    Re: Word and Character counter in VB6?

    @akhileshbc: thanks for your help. By the way, I put your forum name into one of my vb comments.

  11. #11
    Junior Member
    Join Date
    Oct 2012
    Posts
    23

    Re: Word and Character counter in VB6?

    hey there
    I'm new here and this code trapped me, so I tried to make it show the number of word and number of letters for each word in the text, but I failed xD
    can any one help.

  12. #12
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Word and Character counter in VB6?

    First of all you should start a new thread for your question. This one is over a year old.
    Second you need to tell us what you are trying to do and what problem(s) you are having.
    Third you need to show us some of the code you used that is giving you the problem.

    We can't help if we don't have any info on your problem.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width