i have vba code in word iterates through 2 arrays of strings, finds text in document 1 array, , replaces corresponding string in other array so:
with application.activedocument.content.find i=1 100 .text=array1(i) .replacement.text=array2(i) .forward=true .matchcase=true .wrap=wdfindcontinue .matchwholeword=true .matchwildcards=false .matchallwordforms=false .matchprefix=true .matchsuffic=true .matchsoundslike=false .execute replace:=wdreplaceall next end this replaces cases indiscriminately. there way include if clause makes won't replace word if first word in line? i'm not sure vba code testing if first word of line.
any appreciated.
as there isn't conditional in find , replace not replace words @ start of line, there 2 options can think of:
- executing each find statement , checking whether selected word @ start of line before deciding whether replace text
- looping through each line of document , performing find , replace on in line except first word
neither of these particularly elegant, went second option. prefer use range object rather using selection, in instance selection seemed easier work with. note you'll need turn off wrap work.
= 1 100 selection.movestart wdunits.wdstory, count:=-1 selection.moveright unit:=wdword, count:=1 selection.endkey unit:=wdline, extend:=wdextend selection.find .text = array1(i) .replacement.text = array2(i) .forward = true .matchcase = true .matchwholeword = true .matchwildcards = false .matchallwordforms = false .matchprefix = true .matchsuffix = true .matchsoundslike = false .execute replace:=wdreplaceall end selection.movestart unit:=wdline, count:=1 loop until selection.end = activedocument.range.end next
Comments
Post a Comment