I'm having a problem determining the cursor position ("insertion point"?) following EditFind. I also want to use SetSelRange to restore the position to the point prior to the find, but I'm not confident in it either. After all the hours of trial and error, it looks like the code is only 99.5% correct - grr!
It SEEMS to work perfectly if no text is SELECTED, but after doing a successful find, the target is always selected, so ensuing calls malfunction. Hmmm...
Could someone spend 5 minutes trace debugging this code and see if I'm approaching this correctly? Despite its lenghty appearance, it's quick to trace through, as long as you don't use a backslash (best: use "a,e"
NOTE: this had to be written in and for Word version 6.0, in case there is an answer that requires 97 or 2000, though you can probably solve this using them.
This is a pretty cool "OR"-style enhancement of Find - when solved, you may want to keep it
'This is a nonwrapping FORWARD search for "OR" arguments, comma-separated
'E.g.: Type (without the quotes) "a,e,i,o,u" in the InputBox, and it should go to next vowel
'Go tools/options/Wrap to Window for best code viewing
'problems: (1)want static sResp$ (2) GetSelStartPos() returns same thing after each find ???
'Also beware that while debugging, is CODE WINDOW is operated on, not the "active document"?
Dim sResp$ 'can this be global or static,so re-seeds next time?
Sub MAIN 'this is full of Word 6 compromises - sorry!
'sResp$ is dimmed implicitly by the routine. The variable ABOVE the sub is NOT USED.
Dim iInitSelStart, iInitSelEnd, iCommaPos, iSavPos, iTrialPos, iNewPos, iNewPosLen
Dim sStr$, bfound, bLoopForBkSlash
On Error Goto errInputCancel
sResp$ = InputBox$("Type multiple ' Or ' Find items, comma separated ( Use \, for actual comma) ", "MultiFind", sResp$)
labelAfterInputBox: 'Word 6: no Resume Next statement!!!
On Error Goto 0
If Right$(sResp$, 1) = Chr$(10) Then 'strip user's LF/CR
sResp$ = Left$(sResp$, Len(sResp$) - 2)
EndIf
If sResp$ = "" Then Goto labelExitSub 'Word 6: no exit sub!!!
iInitSelStart = GetSelStartPos() : iInitSelEnd = GetSelEndPos() 'for ensuing Finds
iNewPos = iInitSelStart 'our target - where we will go when done
iNewPosLen = iInitSelEnd - iInitSelStart + 1
iCommaPos = 1 : iSavPos = 1 : bFound = 0
While iCommaPos > 0 'always do at least one Find
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 = 0 Else bLoopForBkSlash = 1
While bLoopForBkSlash > 0'GRR!No exit while.Can't"while Mid$";iCommaPos may be 1
If Not Mid$(sResp$, iCommaPos - 1, 1) = "\" Then bLoopForBkSlash = 0
If bLoopForBkSlash > 0 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$, "," 'not (iCommaPos+1,
If iCommaPos = 0 Then bLoopForBkSlash = 0
EndIf
Wend
If iCommaPos > 0 Then sStr$ = Mid$(sResp$, iSavPos, iCommaPos - iSavPos)
iSavPos = iCommaPos + 1
EndIf
If sStr$ <> "" Then EditFind .Find = sStr$, .Direction = 0, .Wrap = 0
' MsgBox "<" + sStr$ + "> " + Str$(EditFindFound())
'Stop
If EditFindFound() Then
bFound = 1 : iTrialPos = GetSelStartPos()
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$)
EndIf
EndIf
SetSelRange(iInitSelStart, iInitSelEnd) 'back to start for next
Wend
SetSelRange(iNewPos, iNewPos + iNewPoslen) 'is this correct?
If bfound = 0 Then MsgBox "Solly, no, Cholly"
Goto labelExitSub 'Word 6: no exit sub!!!
errInputCancel:
If err <> 102 Then Error err
Goto labelExitSub
labelExitSub:
End Sub
It SEEMS to work perfectly if no text is SELECTED, but after doing a successful find, the target is always selected, so ensuing calls malfunction. Hmmm...
Could someone spend 5 minutes trace debugging this code and see if I'm approaching this correctly? Despite its lenghty appearance, it's quick to trace through, as long as you don't use a backslash (best: use "a,e"
NOTE: this had to be written in and for Word version 6.0, in case there is an answer that requires 97 or 2000, though you can probably solve this using them.
This is a pretty cool "OR"-style enhancement of Find - when solved, you may want to keep it
'This is a nonwrapping FORWARD search for "OR" arguments, comma-separated
'E.g.: Type (without the quotes) "a,e,i,o,u" in the InputBox, and it should go to next vowel
'Go tools/options/Wrap to Window for best code viewing
'problems: (1)want static sResp$ (2) GetSelStartPos() returns same thing after each find ???
'Also beware that while debugging, is CODE WINDOW is operated on, not the "active document"?
Dim sResp$ 'can this be global or static,so re-seeds next time?
Sub MAIN 'this is full of Word 6 compromises - sorry!
'sResp$ is dimmed implicitly by the routine. The variable ABOVE the sub is NOT USED.
Dim iInitSelStart, iInitSelEnd, iCommaPos, iSavPos, iTrialPos, iNewPos, iNewPosLen
Dim sStr$, bfound, bLoopForBkSlash
On Error Goto errInputCancel
sResp$ = InputBox$("Type multiple ' Or ' Find items, comma separated ( Use \, for actual comma) ", "MultiFind", sResp$)
labelAfterInputBox: 'Word 6: no Resume Next statement!!!
On Error Goto 0
If Right$(sResp$, 1) = Chr$(10) Then 'strip user's LF/CR
sResp$ = Left$(sResp$, Len(sResp$) - 2)
EndIf
If sResp$ = "" Then Goto labelExitSub 'Word 6: no exit sub!!!
iInitSelStart = GetSelStartPos() : iInitSelEnd = GetSelEndPos() 'for ensuing Finds
iNewPos = iInitSelStart 'our target - where we will go when done
iNewPosLen = iInitSelEnd - iInitSelStart + 1
iCommaPos = 1 : iSavPos = 1 : bFound = 0
While iCommaPos > 0 'always do at least one Find
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 = 0 Else bLoopForBkSlash = 1
While bLoopForBkSlash > 0'GRR!No exit while.Can't"while Mid$";iCommaPos may be 1
If Not Mid$(sResp$, iCommaPos - 1, 1) = "\" Then bLoopForBkSlash = 0
If bLoopForBkSlash > 0 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$, "," 'not (iCommaPos+1,
If iCommaPos = 0 Then bLoopForBkSlash = 0
EndIf
Wend
If iCommaPos > 0 Then sStr$ = Mid$(sResp$, iSavPos, iCommaPos - iSavPos)
iSavPos = iCommaPos + 1
EndIf
If sStr$ <> "" Then EditFind .Find = sStr$, .Direction = 0, .Wrap = 0
' MsgBox "<" + sStr$ + "> " + Str$(EditFindFound())
'Stop
If EditFindFound() Then
bFound = 1 : iTrialPos = GetSelStartPos()
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$)
EndIf
EndIf
SetSelRange(iInitSelStart, iInitSelEnd) 'back to start for next
Wend
SetSelRange(iNewPos, iNewPos + iNewPoslen) 'is this correct?
If bfound = 0 Then MsgBox "Solly, no, Cholly"
Goto labelExitSub 'Word 6: no exit sub!!!
errInputCancel:
If err <> 102 Then Error err
Goto labelExitSub
labelExitSub:
End Sub