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|beyond|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
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,
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_English_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.