Jay
Mon Aug 25 18:59:31 PDT 2008
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
On Mon, 25 Aug 2008 11:27:28 -0700 (PDT), levydav <CCarcohen@gmail.com> 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 <jay.freed...@verizon.net> wrote:
>> On Sun, 24 Aug 2008 11:56:53 -0400, "David" <n...@levydav.fastmail.fm> 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_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.
--
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.