Word VBA: Modifying words by looping ActiveDocument.Words creates an infinte loop -
i want loop through word document word word. i'm using activedocument.words collection seems pretty simple. but, there strange issue if change content of 1 word, internal pointer doesn't move next word instead point's once more same word modified. , in cases creates situation routine stuck in looping on single word infinitely.
an example code:
sub loopwords() dim wd range = 0 each wd in activedocument.words if wd.text = "foo " wd.text = wd.text & "bar " end if 'prevent infinite loop: = + 1 if > 99 exit end if next wd end sub
so example using macro in doc containing phrase "there foo in here." results in "there foo bar bar bar bar bar bar bar bar bar bar bar bar bar bar ..." , on.
so why on earth behave this?
how can loop word word , modify text when necessary?
your macro replacing "foo" "foo bar" , causing read "foo" new word on , on again.
instead of:
wd.text = wd.text & "bar "
try:
wd.insertafter "bar "
Comments
Post a Comment