Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / August 2005

Tip: Looking for answers? Try searching our database.

Pulling out words in capitals from word doc

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gem_man - 01 Aug 2005 00:27 GMT
Plesae could someone help me out with some code to extract words that appear
in CAPITAL LETTERS in a word document?

I cannot find any reference to this in VBA help files.

Many thanks
Jay Freedman - 01 Aug 2005 00:53 GMT
>Plesae could someone help me out with some code to extract words that appear
>in CAPITAL LETTERS in a word document?
>
>I cannot find any reference to this in VBA help files.
>
>Many thanks

The general approach is to use a wildcard search (see
http://www.gmayor.com/replace_using_wildcards.htm) and search for the
expression

  <[A-Z]@>

That will find each word that consists of only upper case letters.

What you do with the word once you find it depends on what you mean by
"extract". Do you want to copy them to another document?

--
Regards,
Jay Freedman
Microsoft Word MVP         FAQ: http://word.mvps.org
Gem_man - 01 Aug 2005 06:23 GMT
Thanks Jay,

Yes, copy them them to a new word. The ultimate intention is to arrange
these words in alphabetical order.

regards

> >Plesae could someone help me out with some code to extract words that appear
> >in CAPITAL LETTERS in a word document?
[quoted text clipped - 18 lines]
> Jay Freedman
> Microsoft Word MVP         FAQ: http://word.mvps.org
Helmut Weber - 01 Aug 2005 10:48 GMT
Hi,

you might have to take care of words
containing lowercase letters which were formatted
as .Font.AllCaps = True in addition.

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
Doug Robbins - 01 Aug 2005 12:58 GMT
The following should do what you want:

Dim Source As Document, Target As Document, Capword As Range
Set Source = ActiveDocument
Set Target = Documents.Add
Source.Activate
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
   Do While .Execute(FindText:="[A-Z]{2,}[ .]{1}", MatchWildcards:=True,
Wrap:=wdFindStop, Forward:=True) = True
       Set Capword = Selection.Range
       If Right(Capword, 1) = "." Then
           Capword.End = Capword.End - 1
       End If
       Target.Range.InsertAfter Capword.Text & vbCr
   Loop
End With
Target.Activate
Target.Content.Sort SortOrder:=wdSortOrderAscending

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> Thanks Jay,
>
[quoted text clipped - 26 lines]
>> Jay Freedman
>> Microsoft Word MVP         FAQ: http://word.mvps.org
Gem_man - 01 Aug 2005 23:55 GMT
Thanks for that Doug. It worked a treat.

I am trying to do other things with this. I have managed to operate the code
from a userform and have managed to give the option of the CAPITAL words
being arranged in vertical or horizontal format.

Would you be so kind as to give me tip on how to be able to count the unique
occurences of each word? Also i want to be able to programatically delete all
duplicates of a word.

For some reason my Answer Wizard section of the VBA help is blank so finding
a few hints is proving extremely difficult

Many thanks

> The following should do what you want:
>
[quoted text clipped - 47 lines]
> >> Jay Freedman
> >> Microsoft Word MVP         FAQ: http://word.mvps.org
Jay Freedman - 02 Aug 2005 02:01 GMT
Extending Doug's macro, replace the last two lines with this:

   Dim i As Long
   With Target
       .Content.Sort SortOrder:=wdSortOrderAscending
       For i = .Paragraphs.Count To 2 Step -1
           If .Paragraphs(i).Range.Text = _
                 .Paragraphs(i - 1).Range.Text Then
               .Paragraphs(i).Range.Delete
           End If
       Next i
       Do While Len(.Paragraphs(1).Range.Text) = 1
           .Paragraphs(1).Range.Delete
       Loop
       .Activate
       MsgBox "Found " & .Paragraphs.Count & " unique words"
   End With

The earlier code puts each word in a separate paragraph (because of
the expression "& vbCr" in the InsertAfter line). The For loop
compares each paragraph, starting with the last one, to the paragraph
that precedes it. If they match, the later one of the pair is deleted.
The loop continues until the second word is compared to the first one.

The Do While loop removes any empty paragraphs, which sort to the
beginning of the document; there will always be at least one.

Finally, the MsgBox displays the paragraph count, which is now also
the count of unique words.

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

>Thanks for that Doug. It worked a treat.
>
[quoted text clipped - 62 lines]
>> >> Jay Freedman
>> >> Microsoft Word MVP         FAQ: http://word.mvps.org
Gem_man - 02 Aug 2005 08:25 GMT
Thanks for that Jay. This is great. From your code I think I have figured how
to count the number of times the same word appears too  :-)

One further question. Have either you or Doug got any hints on how I can
pull not only the words that are in captal letters off the original doc but
also the preceeding 1 or maybe 2 words (irrespective of the lettering case).

I think with this info I will have enough to work out the rest of the stuff
myself... he said hopefully.

Thanks again for your assistance, I am very grateful

> Extending Doug's macro, replace the last two lines with this:
>
[quoted text clipped - 97 lines]
> >> >> Jay Freedman
> >> >> Microsoft Word MVP         FAQ: http://word.mvps.org
Doug Robbins - 02 Aug 2005 12:52 GMT
Replacing the

[A-Z]{2,}[ .]{1}

with

[A-z]{1,} [A-z]{1,} [A-Z]{2,}[ .]{1}

will result in the capitalised word and the two preceding words to be found.
Note that getting the sort on the capitalised words will be another story.

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> Thanks for that Jay. This is great. From your code I think I have figured
> how
[quoted text clipped - 122 lines]
>> >> >> Jay Freedman
>> >> >> Microsoft Word MVP         FAQ: http://word.mvps.org
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.