Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS-Word: Search for capital letters in a word

Status
Not open for further replies.

mmtraining

IS-IT--Management
Mar 18, 2002
104
DE
I have to rewrite a very old bit of programming for my own documents. Blaaaaah!

The aim of what I want to produce is to create a table of abbreviations with their explanations in various documents. Is there anything flying around that already does that?

Otherwise I would like to know how I can search long documents for words that contain two or more capital letters and copy them into a table.

Thank you very much for any help. :)

Carol Hammett
Berlin, Germany
 
quickly written so don't take my head off...
this piece of code will find words consisting of two or more capital letters and store them in a collection (sans duplicates)

Code:
Option Explicit

Sub test()
  Dim cAbbrevs As Collection
  Dim oRange As Range
  Dim i As Long
  
  Set cAbbrevs = New Collection
  
  For Each oRange In ActiveDocument.StoryRanges
    With oRange.Find
      .ClearFormatting
      .Replacement.Text = ""
      .Text = &quot;<[A-Z]{2,}>&quot;
      .MatchWildcards = True
      .Format = False
      .Forward = True
      .Wrap = wdFindStop
    End With
    Do While oRange.Find.Execute
      AddToList cAbbrevs, oRange.Text
    Loop
  Next oRange
  
  For i = 1 To cAbbrevs.Count
    Debug.Print cAbbrevs(i)
  Next i
End Sub

Private Sub AddToList(ByRef cColl As Collection, ByVal sItem As String)
  On Error Resume Next
  cColl.Add sItem, sItem
  On Error GoTo 0
End Sub
 
Thanx a lot, Justin,

I can't seem to get VBA to put any of the found words into the list. *Waaaagh* What are the symbols <> in the line .Text = &quot;<[A-Z]{2,}>&quot; for?


I got an error message on the line: Do While oRange.Find.Execute, there doesn't seem to be a method Execute for this object. Hmmm....

If I find anything, will post it here, otherwise, any suggestions?

Carol Hammett :)
 
the < means 'at start of the word'
the > means 'at end of the word'

so <[A-Z]{2,}> means find whole words consisting of 2 or more capital letters

what message are you getting?

Are you running the code from a macro in Word or from a macro in Excel or another MS Application?

Are you running the code from VB using automation?

What version of Word are you using?
I am using Word 97 but code should still work on higher versions (I think)
 
Hi, Justin,

Hang on, have found out what is wrong. I have a mixed language version of Office 97. I replaced the comma in the search text with a semi colon and it now works. Thanx a lot.

I have started to think in another direction, as in Germany abbreviations are sometimes a mixture of large and small letters or even two parts: ESt. or SGB V, which means that only searching for a simple string isn't going to work in all cases.

I am thinking of getting the user to mark the abbreviations he uses with a certain style, which I can then find very fast and stick into a table. What do you think?

If you want, I can give you that solution when I get it done. :)

Carol
Berlin, Germany
 
If you can get the user to use styles for abbreviations then use that as trying to get abbreviations such as the ones you mentioned can get onerous; not to mention prone to errors.
If not then you should look more at Wildcard Searching for MS Word.
Another possibility is Regular Expressions (works similar to Word Wildcards).
Good luck getting them to use styles.
 
Thanx for wishing me good luck in getting the users to use styles. I am about to construct a system of zapping users with 9 million amps if they make a mistake when using one of our document templates. Won't mention it in the handbook, might be bad for the level of acceptance.

My way of thinking is that we r offering them a tool to make their lives easier. If they don't use the tool, that's not our problem. As far as I'm concerned, they can look through 50 pages of text, find an abbreviation, create a line in a table, write the abbreviation into the left column, work out what the long form is, check it and then type it in. No skin off my nose. HA! Now you know.

Carol
Berlin, Germany
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top