Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WORD enhancement to Find command - compound search 2

Status
Not open for further replies.

larryww

Programmer
Mar 6, 2002
193
US
This is code to search for several terms in MS Word. For [a stupid but illustrative] example, if you want to find the next vowel in a .DOC, give this sub arguments a,e,i,o,u. It uses comma as a delimiter, but allows backslash/comma for a literal comma.

I believe this to be reliable code for Word 2000. If improvements, corrections, enhancements are made, the code at the bottom of this thread will be the best to download.

Please note the comments atop code.

This goes atop your normal.dot macros:
Dim sResp$

and this goes anywhere else in your normal.dot macros:
'This is a nonwrapping FORWARD search for "OR" arguments, comma-separated.
'It IGNORES current selection (except to change it when the search succeeds).
'Notice that it uses the static variable sResp$, defined atop this module
'Usage e.g.: Type (without the quotes) "a,e,i,o,u" in the InputBox, and it should go to next vowel
'Also beware that while debugging, is CODE WINDOW is operated on, not the "active document"?
Sub CompoundFind()
Dim iInitSelStart&, iInitSelEnd&, iCommaPos&, iSavPos&, iTrialPos&, iNewPos&, iNewPosLen&
Dim sStr$, bFound As Boolean, bLoopForBkSlash As Boolean
sResp$ = InputBox$("Type multiple ' Or ' Find items, comma separated " _
& Chr(10) & "( Use \, for actual comma) ", "MultiFind", sResp$)
If sResp$ = "" Then Exit Sub
iInitSelStart = Selection.Start: iInitSelEnd = Selection.End 'for ensuing Finds
iNewPos = iInitSelStart 'our target - where we will go when done
iNewPosLen = iInitSelEnd - iInitSelStart + 1
iCommaPos = 1: iSavPos = 1: bFound = False
While iCommaPos > 0 'always do at least one Find
'aha! The next line is the key to working properly. Remove it, and you get bad results
'It would be great if someone could explain what happens when next line is commented
Selection.Start = iInitSelStart + 1: Selection.End = iInitSelStart + 1
iCommaPos = InStr(iSavPos, sResp$, ",") 'this or next Instr will decide the Wending
If iSavPos <= Len(sResp$) Then sStr$ = Mid$(sResp$, iSavPos) 'provisional
If iCommaPos > 0 Then 'to restate sStr$ (if iCommaPos reveals a DELIMITING comma)
If iCommaPos = 1 Then bLoopForBkSlash = False Else bLoopForBkSlash = True
While bLoopForBkSlash 'GRR!No exit while.Can't&quot;while Mid$&quot;;iCommaPos may be 1
If Not Mid$(sResp$, iCommaPos - 1, 1) = &quot;\&quot; Then bLoopForBkSlash = False
If bLoopForBkSlash Then 'i.e., the Mid$ found it
sResp$ = Left$(sResp$, iCommaPos - 2) + Mid$(sResp$, iCommaPos)
sStr$ = Mid$(sResp$, iSavPos) 'again provisionally in case no commas
iCommaPos = InStr(iCommaPos, sResp$, &quot;,&quot;) 'not (iCommaPos+1,
If iCommaPos = 0 Then bLoopForBkSlash = False
End If
Wend
If iCommaPos > 0 Then sStr$ = Mid$(sResp$, iSavPos, iCommaPos - iSavPos)
iSavPos = iCommaPos + 1
End If
If sStr$ <> &quot;&quot; Then
With Selection.Find
.Forward = True: .Wrap = wdFindStop: .Execute FindText:=sStr$
End With
End If
' MsgBox &quot;<&quot; + sStr$ + &quot;> &quot; + Str$(Selection.Find.Found)
'Stop
If Selection.Find.Found Then
bFound = True: iTrialPos = Selection.Start
If iNewPos = iInitSelStart Then 'trial's our very first hit - save it
iNewPos = iTrialPos: iNewPosLen = Len(sStr$)
Else 'else iNewPos DID have prior value; update if sooner in file
If iTrialPos < iNewPos Then iNewPos = iTrialPos: iNewPosLen = Len(sStr$)
End If
End If
Selection.Start = iInitSelStart: Selection.End = iInitSelEnd
Wend
If bFound Then Selection.Start = iNewPos: Selection.End = iNewPos + iNewPosLen _
Else MsgBox &quot;Solly, no, Cholly&quot;
End Sub
 
I know that threads can slide by without being seen ... I'm wondering if anyone took a gander at this? [sadeyes]
 
glanced at it a bit.
have an idea of what it does.
maybe my understanding of what is does is wrong but...
could not the same be done using wildcards?

Code:
Examples of search wildcards

You can fine-tune a search by using any of the following wildcards. In the Find or Replace dialog box, click More if you don't see the Use wildcards check box. Then select the Use wildcards check box and type the wildcard and any other text in the Find what box.

Tip   For a quick way to enter a wildcard in the Find what box, click Special and then click a wildcard.

To find	Use this
wildcard	
Examples
Any single character	?	s?t finds &quot;sat&quot; and &quot;set.&quot;
Any string of characters	*	s*d finds &quot;sad&quot; and &quot;started.&quot;
One of the specified characters	[ ]	w[io]n finds &quot;win&quot; and &quot;won.&quot;
Any single character in this range	[-]	 [r-t]ight finds &quot;right&quot; and &quot;sight.&quot; Ranges must be in ascending order.
Any single character except the characters inside the brackets	[!]	m[!a]st finds &quot;mist&quot; and &quot;most,&quot; but not &quot;mast.&quot;
Any single character except characters in the range inside the brackets	[!x-z]	t[!a-m]ck finds &quot;tock&quot; and &quot;tuck,&quot; but not &quot;tack&quot; or &quot;tick.&quot;
Exactly n occurrences of the previous character or expression	{n}	fe{2}d finds &quot;feed&quot; but not &quot;fed.&quot;
At least n occurrences of the previous character or expression	{n,}	fe{1,}d finds &quot;fed&quot; and &quot;feed.&quot;
From n to m occurrences of the previous character or expression	{n,m}	10{1,3} finds &quot;10,&quot; &quot;100,&quot; and &quot;1000.&quot;
One or more occurrences of the previous character or expression	@	lo@t finds &quot;lot&quot; and &quot;loot.&quot;
The beginning of a word	<	<(inter) finds &quot;interesting&quot; and &quot;intercept,&quot; but not &quot;splintered.&quot;
The end of a word	>	(in)> finds &quot;in&quot; and &quot;within,&quot; but not &quot;interesting.&quot;
Notes

·	You can use parentheses to group the wildcards and text and to indicate the order of evaluation. For example, search for &quot;<(pre)*(ed)>&quot; to find &quot;presorted&quot; and &quot;prevented.&quot;
·	To search for a character that's defined as a wildcard, type a backslash (\) before the character. For example, search for &quot;\?&quot; to find a question mark.
·	You can use the \n wildcard to search for an expression and then replace it with the rearranged expression. For example, type (Newton) (Christie) in the Find what box and \2 \1 in the Replace with box. Word will find &quot;Newton Christie&quot; and replace it with &quot;Christie Newton.&quot;
 

I think BOTH you guys deserve a BIG THANKS - and STARS. :)

...Dale Watson dwatson@bsi.gov.mb.ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top