
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
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