Find and replace tabs

J

Jenny Fletcher

I am trying to delete tabs from a Word doc using the
following code:

Dim objRange as Word.Range

....

With objRange.Find
.ClearFormatting()
.Forward = True
.Replacement.ClearFormatting()
Dim blnReplaced As Boolean = .Execute
(findtext:="^t",ReplaceWith:=String.Empty,
Format:=True,Replace:=Word.WdReplace.wdReplaceAll)
.Execute()
Do While .Found = True
.Execute()
Loop
End With

The problem is that the Tabs are not being removed and I'm
not sure why. When I changed ReplaceWith to "t" to see if
the tabs were being found, it worked.

Can anyone shed any light on this?

Jenny
 
P

Peter Hewett

Hi Jenny Fletcher

I used the following code:

Public Sub SimpleSandR(ByVal strFind As String, _
ByVal strReplace As String)

With ActiveDocument.Content.Find
.ClearFormatting
.MatchCase = False
.MatchSoundsLike = False
.MatchWholeWord = False
.MatchWildcards = False
.Forward = True
.Wrap = wdFindContinue
.Format = False
.Text = strFind
.Replacement.Text = strReplace
.Execute Replace:=wdReplaceAll
End With
End Sub

and called it like this:
SimpleSandR "^t", ""

This replaced all tab characters in the document body.

HTH + Cheers - Peter
 
L

Larry

Isn't a simple find and replace enough?

Dim r As Range
Set r = Selection.Range

With r.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^t"
.Replacement.Text = ""
.MatchWildcards = False
.Forward = True
.Wrap = wdFindContinue
End With
r.Find.Execute Replace:=wdReplaceAll
 
P

Peter Hewett

Hi Larry

If you want consistent reliable results when using th Find object I've found it prudent to
initialise it thoroughly. What's a few lines of code? I also think that it makes exactly
what you're searching for/replacing unambiguous.

HTH + Cheers - Peter
 
L

Larry

Hi Peter,

You know more about this than I do, but have you ever searched for a tab
and not gotten a tab? Have you ever replaced found text by X and ended
up replacing it by Y?

So I'm interested: where does the possible ambiguity come in to a S&R?

Larry
 
P

Peter Hewett

Hi Larry

Sometimes the Find object can behave strangely (not as you would expect). This has been
attributed to ghost properties from a previous S+R operation. So if you specify *every*
Property you know the object is correctly initialised. This is one of the standard bits
of code I pulled from my library:

Public Sub SearchAndReplaceInStory(ByVal rngStory As Word.Range, _
ByVal strSearch As String, _
ByVal strReplace As String)
Do Until (rngStory Is Nothing)
With rngStory.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = strSearch
.Replacement.Text = strReplace
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False

.Execute Replace:=wdReplaceAll
End With
Set rngStory = rngStory.NextStoryRange
Loop
End Sub

As you can see it sets a few more properties.

HTH + Cheers - Peter
 

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