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 / September 2005

Tip: Looking for answers? Try searching our database.

Macro to find whole words?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Carrie Downes - 09 Sep 2005 16:11 GMT
I received excellent help on this board earlier this year in putting together
a macro that would search a Word document for typesetting codes (in
brackets), copy them, and export them to a list in a separate document. I'm
curious as to whether I can take this a step further and pull out the actual
words in which these bracketed codes occur.

What I need is a macro that will search a word document for a list of codes
(things like <#a>, <#s>, <%'>, <%e>, etc.) and then copy the words they occur
in to a separate document. These are typesetting codes used to represent
characters with diacritics in a literary manuscript that is full of foreign
words (mainly transliterated Arabic text), and I'd like to be able to list
all the words found in the manuscript that will include these special codes.
Is there any way to do this? Is there a way to extend the search for the
bracketed codes to extend to include the surrounding text?

I'm a newbie when it comes to working with macros, so any ideas would be
welcome. Thanks in advance!
Klaus Linke - 09 Sep 2005 22:50 GMT
>I received excellent help on this board earlier this year in putting
>together
[quoted text clipped - 20 lines]
> I'm a newbie when it comes to working with macros, so any ideas would be
> welcome. Thanks in advance!

Hi Carrie,

So we don't need to start from scratch, you got the macro below to extract
all tags to a separate document:

Dim sBraces As String
Dim oDoc As Document
With Selection
   .HomeKey Unit:=wdStory
   With .Find
       .ClearFormatting
       .Text = "\<*\>"
       .MatchWildcards = True
       Do While .Execute
           sBraces = sBraces & Selection.Text & vbCrLf
       Loop
   End With
End With

Set oDoc = Documents.Add
oDoc.Range.InsertAfter sBraces
oDoc.Range.Sort

You only need to add some code to get the word, once you found the tag, and
remember that word in sBraces:

Sub DaveLettsMacro()
Dim sBraces As String
Dim oDoc As Document
With Selection
 .HomeKey Unit:=wdStory
 With .Find
   .ClearFormatting
   .Text = "\<*\>"
   .MatchWildcards = True
   Do While .Execute
     Call SelectWord
     sBraces = sBraces & Selection.Text & vbCrLf
     Selection.Collapse (wdCollapseEnd)
   Loop
 End With
End With
Set oDoc = Documents.Add
oDoc.Range.InsertAfter sBraces
oDoc.Range.Sort
End Sub

Sub SelectWord()
 Dim strStop As String
 Dim i As Long
 ' List of all characters that may appear between words:
 strStop = ". !?/" & vbCr & vbTab & Chr(11)
 Selection.MoveStartUntil Cset:=strStop, Count:=wdBackward
 Selection.MoveEndUntil Cset:=strStop, Count:=wdForward
End Sub

There might be a lot of complications, say if your tags can contain
characters that also are word delimiters (punctuation).

Word and VBA can deal with "words", but they think that characters like <
and > can not appear inside a word.
So as long as you have the tags, you are fighting Word.

If you'd replace the tags with the actual characters first, things would be
a bit simpler.
For example, you could use Selection.Words(1).Select instead of the macro
SelectWord() above.

Regards,
Klaus
 
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.