Typing versus replacing

O

ondy

Hi all,
I'm having what is a problem trying to create a macro that uses tabs, which
will later be used in converting text to a table.
It's fine when I use search and replace to place tabs, using ^t, when I know
exactly what the search will find.
But when I search for a piece of code, followed by a digit, I can't use S&R
'cos I don't know what the digit will be.
So I search, then cancel the search and just insert the ^t at the correct
spot using Selection.TypeText Text:="^t".
However instead of getting a tab, the ^t is exactly what I get as a result.
Can anyone help with this dumb problem? Code follows.
Cheers, Terry


Sub TempDate()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "<hr size=1 align=left width=45%>"
.Replacement.Text = "^t<hr size=1 align=left width=45%>^t"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "<br>^#"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
While Selection.Find.Found
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="^t"
Selection.Find.Execute
Wend
End Sub
 
G

Greg

Ondy,

I would use a wildcard to search for your second pattern. Divide it
into groups (<br>)([0-9]). In the replace text use group 1 the tab
and group 2

\1^t\\2

Sub TempDate2()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "<hr size=1 align=left width=45%>"
.Replacement.Text = "^t<hr size=1 align=left width=45%>^t"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
.Text = "(\<br\>)([0-9])"
.Replacement.Text = "\1^t\2"
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub

For your other method to work you would need to use vbTab vice ^t
 
D

Dave Lett

Hi Terry,

You were really close. If you chang the line
Selection.TypeText Text:="^t"
TO
Selection.TypeText Text:=vbTab
Then, your routine will work.

HOWEVER, Greg's routine is far more efficient/elegant, and I strongly
recommend that you use it. His routine also might expose you to wildcards for
the first time. A very powerful tool, indeed. If you don't have much
experience with wildcards, then have a look at the article "Finding and
replacing characters using wildcards" at
http://word.mvps.org/faqs/general/UsingWildcards.htm.
After reviewing the article, you might find that you can do a wildcard S&R
instead of two separate S&Rs. Good luck!

HTH,
Dave
 
O

ondy

Greg and Dave,
Many thanks to you both for your fast and very helpful replies.
Cheers, Terry
 

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