Macro to find whole words?

C

Carrie Downes

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

Klaus Linke

Carrie Downes said:
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!



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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top