View Single Post
  #4   Report Post  
Posted to microsoft.public.word.newusers
Jay Freedman Jay Freedman is offline
external usenet poster
 
Posts: 9,854
Default searching for multiple alternative string in MS Word?

This macro should do what you want. Notice that I've left it up to you to finish
constructing the list of prepositions that should be matched -- I don't know how
many of them you want to include. Notice also that the way the macro is written,
you can't use any of the two-word or three-word prepositions ("according to",
etc.).

See http://www.gmayor.com/installing_macro.htm if needed.

Sub PrepositionWhich()
Dim oRg As Range
Dim PrepList As String

PrepList = "|about|above|across|against|along|among|around|as |at"
PrepList = PrepList & _
"|before|behind|below|beneath|beside|between|beyon d|by|"
' Continue this kind of assignment until PrepList contains
' all the prepositions you want to look for.
' Each word must have a | character before and after it, no spaces.

Set oRg = Selection.Range
If Selection.Type wdSelectionIP Then oRg.Collapse wdCollapseEnd
oRg.End = ActiveDocument.Range.End

With oRg.Find
.ClearFormatting
.Text = "[A-Za-z]@ which"
.MatchWildcards = True
.Format = False
.Forward = True
.Wrap = wdFindStop

Do
.Execute
If InStr(PrepList, "|" & Trim(oRg.Words(1)) & "|") Then
oRg.Select
Exit Sub
Else
oRg.Collapse wdCollapseEnd
End If
Loop Until Not .Found

MsgBox "No more occurrences."
End With
End Sub


On Mon, 25 Aug 2008 11:27:28 -0700 (PDT), levydav wrote:

Yes, I want to include this function in a macro. I have written macros
that employ MS Word's wildcard search, but I don't know how to compare
part of a found string with a separate list of words. I would be
search for words without punctuation before "which"--e.g., "([A-z]@)
which"

Thanks for your help,



On Aug 24, 12:59*pm, Jay Freedman wrote:
On Sun, 24 Aug 2008 11:56:53 -0400, "David" wrote:
I want to search for all occurrences of "which" in a document thatare not
preceeded by prepositions. Is there any way to accurately do this with MS
Word's wildcard search?


Thanks


Not with wildcards -- there is no wildcard for "preposition".

It would be possible to write a macro that finds each occurrence of "which" and
compares the preceding word to a list of all prepositions (such as that in
http://en.wikipedia.org/wiki/List_of..._prepositions).

What do you want to do after you find an occurrence? Also, does it make any
difference if there is punctuation between the occurrence and the preceding
word? Should the search be case-sensitive? Questions such as these should be
answered before starting to design the macro.


--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.