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 / January 2007

Tip: Looking for answers? Try searching our database.

Create Search and Replace with the Replacement text from an Inputbox

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Kim - 07 Jan 2007 15:47 GMT
I need to create a macro to search for all ^t with format style TOC 1
and replace with ^t and text from a user inputbox. HELP!
Doug Robbins - Word MVP - 07 Jan 2007 16:10 GMT
If it's the same text that has to be inserted each time, use:

Dim srange As Range
Dim rtext As String
rtext = InputBox("Enter the text to be inserted")
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
   Do While .Execute(FindText:="^t", MatchWildcards:=False, _
       MatchCase:=False, Wrap:=wdFindStop, Forward:=True) = True
       Set srange = Selection.Range.Duplicate
       Selection.Collapse wdCollapseEnd
       Selection.MoveRight wdCharacter, 1
       If srange.Style = "TOC 1" Then
           srange.Text = vbTab & rtext
       End If
   Loop
End With

If the text changes for each instance, then you will need to move the rtext
= InputBox("Enter the text to be inserted") so that it is before the
srange.Text = vbTab & rtext line of code.

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

>I need to create a macro to search for all ^t with format style TOC 1
> and replace with ^t and text from a user inputbox. HELP!
Kim - 07 Jan 2007 19:56 GMT
> If it's the same text that has to be inserted each time, use:
>
[quoted text clipped - 29 lines]
> >I need to create a macro to search for all ^t with format style TOC 1
> > and replace with ^t and text from a user inputbox. HELP!

Doug,

This worked perfectly!  Thanks so much for your help!!
 
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.