Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2006

Tip: Looking for answers? Try searching our database.

Typing versus replacing

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ondy - 14 Feb 2006 10:43 GMT
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
Greg - 14 Feb 2006 12:37 GMT
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
Dave Lett - 14 Feb 2006 14:14 GMT
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

> Hi all,
> I'm having what is a problem trying to create a macro that uses tabs, which
[quoted text clipped - 48 lines]
> Wend
> End Sub
ondy - 14 Feb 2006 15:50 GMT
Greg and Dave,
Many thanks to you both for your fast and very helpful replies.
Cheers, Terry
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.