Replacing text with a loop using wildcards?

E

Ed from AZ

*sigh* I know I've done this before, but I can't find my code and
it's been too long!

My writers constantly present me with a "bulleted" list that needs to
be replaced with a numberred list. The "bullets" consist of a series
of spaces, a hyphen, and a few more spaces, always at the beginning of
the line. I can not use automatic numbering. I am trying to run
through a range using Find with Wildcards = True and replace the text
with what I need. But this is what I've come up with and it won't
work.

x = 0
With rng.Find
.ClearFormatting
.MatchWildcards = True
If .Execute(FindText:="Chr(13) & {Chr(32)}[2,] & " - " & {Chr(32)}
[1,]") Then
x = x + 1
str = "Chr(13) & (" & x & ") "
rng.Text = str
End If
End With

What have I forgotten?

Ed
 
D

Doug Robbins - Word MVP

Try

Dim i As Long
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
i = 1
Do While .Execute(Findtext:="^13[ ]{2,}-[ ]{2,}", Forward:=True, _
MatchWildcards:=True, Wrap:=wdFindContinue) = True
Selection.Text = vbCr & i & vbTab
i = i + 1
Loop
End With


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

Ed from AZ

Thank you, Doug! I assume I can substitute ranges for Selection and
still be okay?

Ed

Try

Dim i As Long
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
i = 1
    Do While .Execute(Findtext:="^13[ ]{2,}-[ ]{2,}", Forward:=True, _
MatchWildcards:=True, Wrap:=wdFindContinue) = True
        Selection.Text = vbCr & i & vbTab
        i = i + 1
    Loop
End With

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



*sigh*  I know I've done this before, but I can't find my code and
it's been too long!
My writers constantly present me with a "bulleted" list that needs to
be replaced with a numberred list.  The "bullets" consist of a series
of spaces, a hyphen, and a few more spaces, always at the beginning of
the line.  I can not use automatic numbering.  I am trying to run
through a range using Find with Wildcards = True and replace the text
with what I need.  But this is what I've come up with and it won't
work.
x = 0
With rng.Find
 .ClearFormatting
 .MatchWildcards = True
   If .Execute(FindText:="Chr(13) & {Chr(32)}[2,] & " - " & {Chr(32)}
[1,]") Then
     x = x + 1
     str = "Chr(13) &      (" & x & ")  "
     rng.Text = str
   End If
End With
What have I forgotten?
Ed- Hide quoted text -

- Show quoted text -
 
D

Doug Robbins - Word MVP

Declare a Range object, set that object to the Range on which you want the
change to be made and then incorporate code to determine if the found
selection is within that range and only make the change if it is.

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

Thank you, Doug! I assume I can substitute ranges for Selection and
still be okay?

Ed

Try

Dim i As Long
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
i = 1
Do While .Execute(Findtext:="^13[ ]{2,}-[ ]{2,}", Forward:=True, _
MatchWildcards:=True, Wrap:=wdFindContinue) = True
Selection.Text = vbCr & i & vbTab
i = i + 1
Loop
End With

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

message

*sigh* I know I've done this before, but I can't find my code and
it's been too long!
My writers constantly present me with a "bulleted" list that needs to
be replaced with a numberred list. The "bullets" consist of a series
of spaces, a hyphen, and a few more spaces, always at the beginning of
the line. I can not use automatic numbering. I am trying to run
through a range using Find with Wildcards = True and replace the text
with what I need. But this is what I've come up with and it won't
work.
x = 0
With rng.Find
.ClearFormatting
.MatchWildcards = True
If .Execute(FindText:="Chr(13) & {Chr(32)}[2,] & " - " & {Chr(32)}
[1,]") Then
x = x + 1
str = "Chr(13) & (" & x & ") "
rng.Text = str
End If
End With
What have I forgotten?
Ed- Hide quoted text -

- Show quoted text -
 

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