Create Search and Replace with the Replacement text from an Inputbox

K

Kim

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!
 
D

Doug Robbins - Word MVP

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.


--
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
 
K

Kim

Doug said:
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.


--
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

Doug,

This worked perfectly! Thanks so much for your help!!
 

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