ms word - How to work with selection -


i trying convert working macro hyperlink activedoccument.range selection.range.

code is

with selection.range   .find     .clearformatting     .replacement.clearformatting     .text = "string string1"     .replacement.text = ""     .forward = true     .wrap = wdfindstop     .format = false     .matchwildcards = true     .execute   end   while .find.found     strtxt = split(.text, " ")(1)     strtxt = right(strtxt, 2) & "/" & left(strtxt, 4) & "/" & mid(strtxt, 8, 2) & "/" & mid(strtxt, 5, 3)     .hyperlinks.add anchor:=.duplicate, address:="address" & strtxt & "/0.pdf", texttodisplay:=.text     .end = .fields(1).result.end     .collapse wdcollapseend     .find.execute   loop end 

how collapse correctly make work. hyperlinks in doccument instead selection.

as far can tell, problem found range's end needs increased 1 when hyperlink inserted. believe have check have not gone past original selection.range end, need additional test.

this seemed ok in tables, (a) testing in mac word 2011, may different, , (b), if select column or noncontiguous ranges, have work lot harder make changes in selection (because of well-known lack of support such selections).

sub fandr() const strtext string = "string string1" dim dr word.range dim sr word.range set sr = selection.range 'debug.print sr.start, sr.end set dr = sr.duplicate ' try deal problem find fails find ' find text if same selection sr.collapse wdcollapsestart sr.find   .clearformatting   .replacement.clearformatting   .text = strtext   .replacement.text = ""   .forward = true   .wrap = wdfindstop   .format = false   .matchwildcards = true   while .execute(replace:=false)     if sr.inrange(dr)       'debug.print sr.start, sr.end, dr.start, dr.end       strtxt = split(.text, " ")(1)       strtxt = right(strtxt, 2) & "/" & left(strtxt, 4) & "/" & mid(strtxt, 8, 2) & "/" & mid(strtxt, 5, 3)       sr.hyperlinks.add anchor:=sr, address:="address" & strtxt & "/0.pdf", texttodisplay:=.text       sr.collapse wdcollapseend       sr.end = sr.end + 1       sr.start = sr.end       'debug.print sr.start, sr.end, dr.start, dr.end     else       exit     end if   loop end set sr = nothing set dr = nothing end sub 

Comments