Word VBA: Find and replace with ref code -
i trying find , replace brackets , text in field code http://office.microsoft.com/en-us/word-help/field-codes-ref-field-hp005186139.aspx
sub replacereftag() replacementtext "<test>", "ref test.test1 " end sub private sub replacementtext(findtext string, replacetext string) selection.find.clearformatting selection.find.replacement.clearformatting selection.find .text = findtext .myrange.field = replacetext .wrap = wdfindcontinue .matchwildcards = true end selection.find.execute replace:=wdreplaceall end sub
in opinion need use different kind of logic in code. main difference need find text first , select it. next step need add field in range of selection. following subroutines improved ones doing these things. hope it's looking for. (see comments inside code)
sub replacereftag() replacementtext "<test>", "ref test.test1 " 'if keep .matchwildcards = true below call next sub in way: 'replacementtext "\<test\>", "ref test.test1 " end sub private sub replacementtext(findtext string, replacetext string) selection.find.clearformatting selection.find.replacement.clearformatting selection.find .text = findtext 'i removed here .wrap = wdfindcontinue .matchwildcards = false '!!!- both <> special characters!! end 'looping search occurrences while selection.find.execute 'add field here selection.fields.add range:=selection.range, type:=wdfieldempty, text:= _ "ref test.test1", preserveformatting:=false loop end sub
edit
sub replacereftag() replacementtext "<test>", "ref test.test" dim i=1 10 'change last number replacementtext "<test" & & ">", "ref test.test" & next end sub
Comments
Post a Comment