Loop not Looping

K

kpm

I am relatively new to writing macros and have a feeling I am missing one
step, but I am trying to write a macro that searches up for an occurrence of
the word article followed by a space and any number and another space. I
then want it to delete the space, and insert a tab. The macro works fine,
but for some reason, it is not looping. If you have any suggestions, I would
greatly appreciate it. here is what I have so far:

Selection.HomeKey Unit:=wdStory
Selection.GoTo What:=wdGoToField, Which:=wdGoToNext, Count:=1, Name:="TOC"
Selection.Find.ClearFormatting
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=1
Do
With Selection.Find
.Text = "article ^# "
.Replacement.Text = ""
.Forward = False
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeBackspace
Selection.TypeText Text:=vbTab
Loop Until Condition = False
End Sub
 
D

Dave Lett

Hi kpm,

Well, you see the thing is, you don't really need a loop. I think the
following takes care of what you're trying to accomplish:

Selection.HomeKey Unit:=wdStory
Selection.GoTo What:=wdGoToField, Which:=wdGoToNext, Count:=1, Name:="TOC"
Selection.Find.ClearFormatting
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=1
With Selection.Find
.Text = "article ^# "
.Forward = False
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
With .Replacement
.ClearFormatting
.Text = "article ^t"
End With
.Execute Replace:=wdReplaceAll
End With


HTH,
Dave
 
K

kpm

Dave, thanks for the repsonse. Because I need the Tab after the number, for
example Article 15, that scenario wouldn't work. I figured it our after I
posted the message. I replaced the Loop with Selection.Find.Found and it
worked perfectly. Thanks.
 
D

Dave Lett

Hi Kpm,

Actually, a slightly different solution would work, would be much faster,
and more efficient. That is, you could use wildcards to do the replace and
never have to loop at all. I know you're "relatively new to writing macros",
so I will just point you to an article that you might find very interesting
and educational:

"Finding and replacing characters using wildcards" at
http://word.mvps.org/faqs/general/UsingWildcards.htm

HTH,
Dave
 

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

Similar Threads


Top