Scanning text & objDoc.Characters.Count 
Author Message
 Scanning text & objDoc.Characters.Count

I'm trying to scan the text of a document, and find all words that are
capitalised. I'm using the following code, which has a problem in that it
stops before the end of the document.

I print out objDoc.Characters.Count, and this displays 107, which is exactly
where the loop ends. However, the loop ends before all the capitalised words
have been found, so there are obviously more than 107 characters. Can anyone
tell me where I'm going wrong?

I'm using Word 97 if that makes a difference.

TIA,
Mark.

    Dim objDoc As Document
    Dim lPosition As Long
    Dim szUpperWord As String
    Dim szWord As String
    Dim szChar As String

    Set objDoc = ThisDocument

    lPosition = 1
    Debug.Print CStr(objDoc.Characters.Count)

    Do While Not objDoc.Characters.Last
        szChar = Mid$(objDoc.Range, lPosition, 1)

        Debug.Print "Char " + CStr(lPosition) + " = " + szChar

        If szChar = " " Or Asc(szChar) = 13 Or Asc(szChar) = 7 Then
            ' ...reached end of word
            If IsUppercase(szWord) Then
                MsgBox ("TLA - " + szWord)
            End If

            szWord = ""
        Else
            ' ...more characters to come
            szWord = szWord + szChar
        End If

        lPosition = lPosition + 1
    Loop



Tue, 21 Sep 2004 15:38:02 GMT  
 Scanning text & objDoc.Characters.Count
Hi Mark
Try this:
==============================
Sub CapitalizedWords()
Dim strWord
Dim i As Integer
For Each strWord In ActiveDocument.Words
    Select Case Asc(Left$(strWord, 1))
        Case 65 To 90
            i = i + 1
    End Select
Next
MsgBox i
End Sub
==============================
Good luck
Harold


Quote:
> I'm trying to scan the text of a document, and find all words that
are
> capitalised. I'm using the following code, which has a problem in
that it
> stops before the end of the document.

> I print out objDoc.Characters.Count, and this displays 107, which is
exactly
> where the loop ends. However, the loop ends before all the
capitalised words
> have been found, so there are obviously more than 107 characters.
Can anyone
> tell me where I'm going wrong?

> I'm using Word 97 if that makes a difference.

> TIA,
> Mark.

>     Dim objDoc As Document
>     Dim lPosition As Long
>     Dim szUpperWord As String
>     Dim szWord As String
>     Dim szChar As String

>     Set objDoc = ThisDocument

>     lPosition = 1
>     Debug.Print CStr(objDoc.Characters.Count)

>     Do While Not objDoc.Characters.Last
>         szChar = Mid$(objDoc.Range, lPosition, 1)

>         Debug.Print "Char " + CStr(lPosition) + " = " + szChar

>         If szChar = " " Or Asc(szChar) = 13 Or Asc(szChar) = 7 Then
>             ' ...reached end of word
>             If IsUppercase(szWord) Then
>                 MsgBox ("TLA - " + szWord)
>             End If

>             szWord = ""
>         Else
>             ' ...more characters to come
>             szWord = szWord + szChar
>         End If

>         lPosition = lPosition + 1
>     Loop



Tue, 21 Sep 2004 19:11:50 GMT  
 Scanning text & objDoc.Characters.Count
Harold,

Thanks for the idea about using a For..Each loop - I'd completely forgotten
about those! I adapted your code slightly, and it works perfectly. I've
copied the code below so you can see what changes I made:

Thanks again,
Mark.

Sub CapitalizedWords()
    Dim szWord As Variant
    Dim szChar As Variant
    Dim bCapital As Boolean
    Dim iPosition As Integer

    For Each szWord In ActiveDocument.Words
        bCapital = True

        ' Remove any spaces
        szWord = Trim(szWord)

        ' Check every character is a capitalised letter
        For iPosition = 1 To Len(szWord)
            If Asc(Mid$(szWord, iPosition, 1)) < 65 Or Asc(Mid$(szWord,
iPosition, 1)) > 90 Then
                bCapital = False
            End If
        Next

        If bCapital = True Then UpdateDefinitionsTable (szWord)
    Next
End Sub


Quote:
> Hi Mark
> Try this:
> ==============================
> Sub CapitalizedWords()
> Dim strWord
> Dim i As Integer
> For Each strWord In ActiveDocument.Words
>     Select Case Asc(Left$(strWord, 1))
>         Case 65 To 90
>             i = i + 1
>     End Select
> Next
> MsgBox i
> End Sub
> ==============================
> Good luck
> Harold



> > I'm trying to scan the text of a document, and find all words that
> are
> > capitalised. I'm using the following code, which has a problem in
> that it
> > stops before the end of the document.

> > I print out objDoc.Characters.Count, and this displays 107, which is
> exactly
> > where the loop ends. However, the loop ends before all the
> capitalised words
> > have been found, so there are obviously more than 107 characters.
> Can anyone
> > tell me where I'm going wrong?

> > I'm using Word 97 if that makes a difference.

> > TIA,
> > Mark.

> >     Dim objDoc As Document
> >     Dim lPosition As Long
> >     Dim szUpperWord As String
> >     Dim szWord As String
> >     Dim szChar As String

> >     Set objDoc = ThisDocument

> >     lPosition = 1
> >     Debug.Print CStr(objDoc.Characters.Count)

> >     Do While Not objDoc.Characters.Last
> >         szChar = Mid$(objDoc.Range, lPosition, 1)

> >         Debug.Print "Char " + CStr(lPosition) + " = " + szChar

> >         If szChar = " " Or Asc(szChar) = 13 Or Asc(szChar) = 7 Then
> >             ' ...reached end of word
> >             If IsUppercase(szWord) Then
> >                 MsgBox ("TLA - " + szWord)
> >             End If

> >             szWord = ""
> >         Else
> >             ' ...more characters to come
> >             szWord = szWord + szChar
> >         End If

> >         lPosition = lPosition + 1
> >     Loop



Tue, 21 Sep 2004 21:03:11 GMT  
 Scanning text & objDoc.Characters.Count
Hi, Mark,

You've picked a particularly inefficient way of checking for all-uppercase words. Whenever you find
yourself writing a loop that looks at every character in a document, you should ask yourself whether
there's a better way -- there almost always is.

In this case, the better way is to use the UCase() function. If you apply it to a string that
contains lower-case letters, it changes them to the upper-case equivalents. If you compare this
result to the original and they're the same, then the original must already be all upper-case; if
they're different, there was at least one lower-case letter. You also need to exclude periods and
paragraph marks, which Word considers to be "words". In code,

     For Each szWord In ActiveDocument.Words
         If (Left(szWord, 1) <> ".") And _
            (szWord <> vbCr) And _
            (szWord = UCase(szWord)) Then
             bCapital = True
         Else
             bCapital = False
         End If

         If bCapital = True Then UpdateDefinitionsTable (Trim(szWord))
     Next

or more simply

     For Each szWord In ActiveDocument.Words
         If (Left(szWord, 1) <> ".") And _
            (szWord <> vbCr) And _
            (szWord = UCase(szWord)) Then
              UpdateDefinitionsTable (Trim(szWord))
         End If
     Next

This loop should run an order of magnitude faster than the character-by-character loop.

--
Regards,
Jay Freedman
Microsoft Word MVP        Word MVP FAQ site: http://www.mvps.org/word

Quote:

> Harold,

> Thanks for the idea about using a For..Each loop - I'd completely forgotten
> about those! I adapted your code slightly, and it works perfectly. I've
> copied the code below so you can see what changes I made:

> Thanks again,
> Mark.

> Sub CapitalizedWords()
>     Dim szWord As Variant
>     Dim szChar As Variant
>     Dim bCapital As Boolean
>     Dim iPosition As Integer

>     For Each szWord In ActiveDocument.Words
>         bCapital = True

>         ' Remove any spaces
>         szWord = Trim(szWord)

>         ' Check every character is a capitalised letter
>         For iPosition = 1 To Len(szWord)
>             If Asc(Mid$(szWord, iPosition, 1)) < 65 Or Asc(Mid$(szWord,
> iPosition, 1)) > 90 Then
>                 bCapital = False
>             End If
>         Next

>         If bCapital = True Then UpdateDefinitionsTable (szWord)
>     Next
> End Sub



> > Hi Mark
> > Try this:
> > ==============================
> > Sub CapitalizedWords()
> > Dim strWord
> > Dim i As Integer
> > For Each strWord In ActiveDocument.Words
> >     Select Case Asc(Left$(strWord, 1))
> >         Case 65 To 90
> >             i = i + 1
> >     End Select
> > Next
> > MsgBox i
> > End Sub
> > ==============================
> > Good luck
> > Harold



> > > I'm trying to scan the text of a document, and find all words that
> > are
> > > capitalised. I'm using the following code, which has a problem in
> > that it
> > > stops before the end of the document.

> > > I print out objDoc.Characters.Count, and this displays 107, which is
> > exactly
> > > where the loop ends. However, the loop ends before all the
> > capitalised words
> > > have been found, so there are obviously more than 107 characters.
> > Can anyone
> > > tell me where I'm going wrong?

> > > I'm using Word 97 if that makes a difference.

> > > TIA,
> > > Mark.

> > >     Dim objDoc As Document
> > >     Dim lPosition As Long
> > >     Dim szUpperWord As String
> > >     Dim szWord As String
> > >     Dim szChar As String

> > >     Set objDoc = ThisDocument

> > >     lPosition = 1
> > >     Debug.Print CStr(objDoc.Characters.Count)

> > >     Do While Not objDoc.Characters.Last
> > >         szChar = Mid$(objDoc.Range, lPosition, 1)

> > >         Debug.Print "Char " + CStr(lPosition) + " = " + szChar

> > >         If szChar = " " Or Asc(szChar) = 13 Or Asc(szChar) = 7 Then
> > >             ' ...reached end of word
> > >             If IsUppercase(szWord) Then
> > >                 MsgBox ("TLA - " + szWord)
> > >             End If

> > >             szWord = ""
> > >         Else
> > >             ' ...more characters to come
> > >             szWord = szWord + szChar
> > >         End If

> > >         lPosition = lPosition + 1
> > >     Loop



Wed, 22 Sep 2004 00:02:00 GMT  
 Scanning text & objDoc.Characters.Count
Jay,

Thanks for the idea - I'm new to VBA, and just happened to see the
characters collection before someone pointed out the words collection,
although I admit I should have realised there would be a better alternative!

Many thanks,
Mark.


Quote:
> Hi, Mark,

> You've picked a particularly inefficient way of checking for all-uppercase

words. Whenever you find
Quote:
> yourself writing a loop that looks at every character in a document, you

should ask yourself whether
Quote:
> there's a better way -- there almost always is.

> In this case, the better way is to use the UCase() function. If you apply
it to a string that
> contains lower-case letters, it changes them to the upper-case

equivalents. If you compare this
Quote:
> result to the original and they're the same, then the original must

already be all upper-case; if
Quote:
> they're different, there was at least one lower-case letter. You also need

to exclude periods and
Quote:
> paragraph marks, which Word considers to be "words". In code,

>      For Each szWord In ActiveDocument.Words
>          If (Left(szWord, 1) <> ".") And _
>             (szWord <> vbCr) And _
>             (szWord = UCase(szWord)) Then
>              bCapital = True
>          Else
>              bCapital = False
>          End If

>          If bCapital = True Then UpdateDefinitionsTable (Trim(szWord))
>      Next

> or more simply

>      For Each szWord In ActiveDocument.Words
>          If (Left(szWord, 1) <> ".") And _
>             (szWord <> vbCr) And _
>             (szWord = UCase(szWord)) Then
>               UpdateDefinitionsTable (Trim(szWord))
>          End If
>      Next

> This loop should run an order of magnitude faster than the

character-by-character loop.
Quote:

> --
> Regards,
> Jay Freedman
> Microsoft Word MVP        Word MVP FAQ site: http://www.mvps.org/word




- Show quoted text -

Quote:
> > Harold,

> > Thanks for the idea about using a For..Each loop - I'd completely
forgotten
> > about those! I adapted your code slightly, and it works perfectly. I've
> > copied the code below so you can see what changes I made:

> > Thanks again,
> > Mark.

> > Sub CapitalizedWords()
> >     Dim szWord As Variant
> >     Dim szChar As Variant
> >     Dim bCapital As Boolean
> >     Dim iPosition As Integer

> >     For Each szWord In ActiveDocument.Words
> >         bCapital = True

> >         ' Remove any spaces
> >         szWord = Trim(szWord)

> >         ' Check every character is a capitalised letter
> >         For iPosition = 1 To Len(szWord)
> >             If Asc(Mid$(szWord, iPosition, 1)) < 65 Or Asc(Mid$(szWord,
> > iPosition, 1)) > 90 Then
> >                 bCapital = False
> >             End If
> >         Next

> >         If bCapital = True Then UpdateDefinitionsTable (szWord)
> >     Next
> > End Sub



> > > Hi Mark
> > > Try this:
> > > ==============================
> > > Sub CapitalizedWords()
> > > Dim strWord
> > > Dim i As Integer
> > > For Each strWord In ActiveDocument.Words
> > >     Select Case Asc(Left$(strWord, 1))
> > >         Case 65 To 90
> > >             i = i + 1
> > >     End Select
> > > Next
> > > MsgBox i
> > > End Sub
> > > ==============================
> > > Good luck
> > > Harold



> > > > I'm trying to scan the text of a document, and find all words that
> > > are
> > > > capitalised. I'm using the following code, which has a problem in
> > > that it
> > > > stops before the end of the document.

> > > > I print out objDoc.Characters.Count, and this displays 107, which is
> > > exactly
> > > > where the loop ends. However, the loop ends before all the
> > > capitalised words
> > > > have been found, so there are obviously more than 107 characters.
> > > Can anyone
> > > > tell me where I'm going wrong?

> > > > I'm using Word 97 if that makes a difference.

> > > > TIA,
> > > > Mark.

> > > >     Dim objDoc As Document
> > > >     Dim lPosition As Long
> > > >     Dim szUpperWord As String
> > > >     Dim szWord As String
> > > >     Dim szChar As String

> > > >     Set objDoc = ThisDocument

> > > >     lPosition = 1
> > > >     Debug.Print CStr(objDoc.Characters.Count)

> > > >     Do While Not objDoc.Characters.Last
> > > >         szChar = Mid$(objDoc.Range, lPosition, 1)

> > > >         Debug.Print "Char " + CStr(lPosition) + " = " + szChar

> > > >         If szChar = " " Or Asc(szChar) = 13 Or Asc(szChar) = 7 Then
> > > >             ' ...reached end of word
> > > >             If IsUppercase(szWord) Then
> > > >                 MsgBox ("TLA - " + szWord)
> > > >             End If

> > > >             szWord = ""
> > > >         Else
> > > >             ' ...more characters to come
> > > >             szWord = szWord + szChar
> > > >         End If

> > > >         lPosition = lPosition + 1
> > > >     Loop



Fri, 24 Sep 2004 16:13:18 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Finding & Counting Certain Characters in a string

2. Ignore strikethrough text in document character count

3. Counting number of characters in a text box

4. Counting Characters in a text box, and makeing a countdown timer

5. how to scan image in black&white format using kodak image scan control

6. Line length & count for multiline Text Box

7. Interrupting Scan Code / Virtual Key Character - (Keyboard)

8. text compare character by character

9. running character count

10. Counting characters as they are typed

11. counting characters in a string

12. COUNT OF CHARACTERS IN A MAIL

 

 
Powered by phpBB® Forum Software