View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
RB Smissaert RB Smissaert is offline
external usenet poster
 
Posts: 2,452
Default Searching for a substring inside another string - there has to be an easier way!

One simple solution is to do it in Word.
You can launch that without having to make it visible.
Lookup OLE automation.

These bits of Word code will give you the idea:

Function GetDocumentIdentifiers() As Collection

Dim rngWord As Range
Dim oStoryRange As Range

Set GetDocumentIdentifiers = New Collection

For Each oStoryRange In ActiveDocument.StoryRanges
For Each rngWord In oStoryRange.Words
If Len(rngWord.Text) 4 And UCase(Left$(rngWord.Text, 2)) = "QQ"
Then
MakeRealWord rngWord
GetDocumentIdentifiers.Add rngWord.Duplicate
End If
Next
Next

End Function

Sub MakeRealWord(rngWord As Range)
'strips these characters from the end of the range
'-------------------------------------------------
rngWord.MoveEndWhile Cset:=Chr(10) & Chr(13) & _
Chr(9) & Chr(11) & Chr(160) & _
Chr(32), _
count:=wdBackward

End Sub


RBS


"Morris" wrote in message
oups.com...
Hi. I'm looking for ans instance of the substring "tab" inside an
rcvdPack string, but I need to be sure it matches a whole word only, so
if it finds "tablets", "tabs", "protetab", or "taberculosis" it mustn't
replace those. I'm checking if a char before T and char after B is
different from a lowercase letter, but I think for what it does there
should be ans easier method. The mostly are spaces before T and after
B, but there might be brackets, fullstops, asterisks, etc. Sometimes
the the word TAB is put at the end of the rcvdPack that's why I check
the k + 2 = Len(rcvdPack)

That's my code:


k = InStr(1, rcvdPack, "tab") ' replace TAB but not where TABS is
present
If k 0 Then
If Not Asc(Mid(rcvdPack, k - 1, 1)) < 97 And Not
Asc(Mid(rcvdPack, k - 1, 1)) 122 Then
'
Else
If k + 2 = Len(rcvdPack) Then
rcvdPack = Replace(rcvdPack, "tab", "tablets")
ElseIf Not Asc(Mid(rcvdPack, k + 3, 1)) = 97 And Not
Asc(Mid(rcvdPack, k + 3, 1)) <= 122 Then
rcvdPack = Replace(rcvdPack, "tab", "tablets")
End If
End If
End If


Any ideas?

Cheers