Results 1 to 12 of 12
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    counting characters (Word97/SR2)

    Here's a little utility function that counts occurrences of characters within a paragraph range.

    (I'm looking for lines that seem to be candidates for conversion from a WordPerfect tab-table to a regular table; hence my test for vbTab).


    <pre>Public Function intCountChars(rng As Range, strChar As String) As Integer
    intCountChars = 0
    Dim i As Integer
    i = 1
    i = InStr(i, rng.Text, vbTab)
    While i > 0
    intCountChars = intCountChars + 1
    i = InStr(i + 1, rng.Text, vbTab)
    Wend
    End Function
    '
    Sub TESTintCountChars()
    Dim prg As Paragraph
    For Each prg In ActiveDocument.Paragraphs
    Dim rng As Range
    Set rng = prg.Range
    If intCountChars(rng, vbTab) > 2 Then
    rng.Select
    MsgBox "got one!"
    Else
    End If
    Next prg
    End Sub
    </pre>


  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: counting characters (Word97/SR2)

    Chris,
    A couple of thoughts:
    1. Your parameter strChar is never used in your function!
    2. For maximum flexibility, you might want to return a long and add a compare option parameter.
    3. If you used i + len(strChar) rather than i + 1, you could use it efficiently for strings of more than one character.
    Just thought I'd make your life more interesting! <img src=/S/grin.gif border=0 alt=grin width=15 height=15>
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: counting characters (Word97/SR2)

    > A couple of thoughts:

    Did you mean "three comments"? (grin!)


    > Your parameter strChar
    Thank you! I was in the middle of playing around and got as far as making the function utility-able, but didn't make, er use, of the utility-ness.



    >return a long
    I thought of this. I get tired of lng and decided, since it is Monday, to use int. OK. I'll change it.



    > i + len(strChar)
    Super!


    >Just thought I'd make your life more interesting!
    You always do, Rory, you always do.


    Did you find any fault with, say, the comment lines (Very big grin).

  4. #4
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: counting characters (Word97/SR2)

    There was only one and it was blank. Made a nice change, really. <img src=/S/evilgrin.gif border=0 alt=evilgrin width=15 height=15>
    Regards,
    Rory

    Microsoft MVP - Excel

  5. #5
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: counting characters (Word97/SR2)

    <pre>Public Function lngCountChars(strIn As String, strChar As String) As Long
    lngCountChars = 0
    Dim lng As Long
    lng = 1
    lng = InStr(lng, strIn, strChar)
    While lng > 0
    lngCountChars = lngCountChars + 1
    lng = InStr(lng + Len(strChar), strIn, strChar)
    Wend
    'Sub TESTlngCountChars()
    ' Debug.Assert 5 = lngCountChars(" here is a string ", " ")
    ' Debug.Assert 6 = lngCountChars(" here is a string ", " ")
    ' Debug.Assert 6 = lngCountChars(" here is a string ", " ")
    ' Debug.Assert 6 = lngCountChars(" here is a string ", " ")
    ' Debug.Assert 2 = lngCountChars(" here is a string here", "here")
    'End Sub
    End Function</pre>


  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: counting characters (Word97/SR2)

    Or
    <code>
    Public Function lngCountChars(strIn As String, strChar As String) As Long
    lngCountChars = (Len(strIn) - Len(Replace(strIn, strChar, ""))) / Len(strChar)
    End Function</code>

  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: counting characters (Word97/SR2)

    [nitpick]"Replace" is in Word97SR2?[/nitpick]
    Thanks Hans.

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: counting characters (Word97/SR2)

    I assumed that 6 years on, the Word 97 might not apply any more. But you're correct, of course - Replace was introduced in VBA for Office 2000.

  9. #9
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: counting characters (Word97/SR2)

    >Replace was introduced in VBA for Office 2000.
    Yes. One of these days I must .... (grin!)
    Believe it or not, I have a job coming up next week which will require me to slide my old Win98/Word97 IDE into the Big Beige Box and crank 'er up again!

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: counting characters (Word97/SR2)

    I still have a PC with Windows 95 and Office 97 at home, but it must be three or four years ago now that I last plugged it in...

  11. #11
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: counting characters (Word97/SR2)

    You should keep hold of that - for some reason Office 97 seems to be the only version (unless I've missed something) of Office between 95 and 2007 that you can't get with an MSDN subscription. Well, apart from the Mac versions, of course...
    Regards,
    Rory

    Microsoft MVP - Excel

  12. #12
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: counting characters (Word97/SR2)

    Word97 also has better filters (for converting Word Perfect documents) than later versions; especially regarding paragraph numbering. The earlier (97) filters were simpler and appear to yield the numbers, from which it is a simple matter to match strings (LIKE or RegExp) and apply suitable numbered styles.

Posting Permissions

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