How to find control characters with VBA?

D

DaveP

I'm trying to find a particular string in a number of documents, and this
string contains special characters - specificially the carat "^" symbol. I'm
able to open a sample document in Word and do a find on the string I'm
interested in, and I find it with no problem. But when I try to do this
programmatically with VBA, I don't find the string.

This is the code I'm using, which doesn't seem to work:

' there is code before this to select the Word range
(objRange)
objRange.Find.Text = "^tCo^p"
objRange.Find.MatchCase = False

If objRange.Find.Execute = True Then
'Success!!
End If

This code works if I'm searching for normal text, but not when I include the
carats. Does anyone know how this should be done? Thanks in advance!
 
K

Klaus Linke

Hi Dave,

You want to search for the literal text "^tCo^p", not for a tab ^t followed
by "Co" and a paragraph mark ^p, right?

Then use "^^tCo^^p".
You can find the Caret in the Find dialog, under "Special", in case you need
it in the future.

Regards,
Klaus
 
R

Rob

If you record a macro and use the Find dialogue you'll see something like this:

With Selection.Find
.Text = "^tCo^p"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute

Of course, half of that stuff isn't needed but you should be able to find
what you need in it. So you would change your code to look something like...

With Selection.Find
.Text = "^tCo^p"
.MatchCase = False
.MatchWholeWord = False
End With
Selection.Find.Execute

If Selection.Find.Found = True Then
'Success!!
End If
 

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