Range.Find returns true, but didn't find anything

C

cas4

I'm doing a basic search and replace on a Word Document using VB6. I'm searching for wildcards that have the format <%wildcard%> and replacing it with text from a database. The problem is, when the .Execute command is called on the range object, it returns true, but the range still conatains text from the entire document (instead of only containing text that was searched on). Here is the code I'm using:

WildCardFound = True
Do While WildCardFound

Set rng1 = doc.Content
Set rng2 = doc.Content
With rng1.Find
.ClearFormatting
.Text = "<%"
If .Execute = True Then
With rng2.Find
.ClearFormatting
.Text = "%>"
If .Execute = True Then
If rng1.Start < rng2.Start Then
WildCard = doc.Range(rng1.End, rng2.Start).Text
Set rng3 = doc.Range(rng1.Start, rng2.End)

rng3.Text = IIf(IsNull(rec.Fields(WildCard).value), "", rec.Fields(WildCard).value)
If Err.Number <> 0 Then
MsgBox "Field name " & WildCard & " is invalid, unable to continue", vbInformation, "Invalid Field"
LoadReport = False
GoTo CleanUp
End If
Else
WildCardFound = False
End If
Else
WildCardFound = False
End If
End With
Else
WildCardFound = False
End If
End With
Loop

I'm running WinXP and Office XP with all the SP's and updates. Since this code did work several months ago, could a SP (Windows or Office) be the cause of this behavior? If anyone has come across this, please let me know.
Thanks in advance.
 
W

Word Heretic

G'day "cas4" <[email protected]>,

exit the with and it should snap up ok

cas4 said:
I'm doing a basic search and replace on a Word Document using VB6. I'm searching for wildcards that have the format <%wildcard%> and replacing it with text from a database. The problem is, when the .Execute command is called on the range object, it returns true, but the range still conatains text from the entire document (instead of only containing text that was searched on). Here is the code I'm using:

WildCardFound = True
Do While WildCardFound

Set rng1 = doc.Content
Set rng2 = doc.Content
With rng1.Find
.ClearFormatting
.Text = "<%"
If .Execute = True Then
With rng2.Find
.ClearFormatting
.Text = "%>"
If .Execute = True Then
If rng1.Start < rng2.Start Then
WildCard = doc.Range(rng1.End, rng2.Start).Text
Set rng3 = doc.Range(rng1.Start, rng2.End)

rng3.Text = IIf(IsNull(rec.Fields(WildCard).value), "", rec.Fields(WildCard).value)
If Err.Number <> 0 Then
MsgBox "Field name " & WildCard & " is invalid, unable to continue", vbInformation, "Invalid Field"
LoadReport = False
GoTo CleanUp
End If
Else
WildCardFound = False
End If
Else
WildCardFound = False
End If
End With
Else
WildCardFound = False
End If
End With
Loop

I'm running WinXP and Office XP with all the SP's and updates. Since this code did work several months ago, could a SP (Windows or Office) be the cause of this behavior? If anyone has come across this, please let me know.
Thanks in advance.

Steve Hudson

Word Heretic, Sydney, Australia
Tricky stuff with Word or words for you.
wordheretic.com

Replies offlist may require payment.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top