Getting the previous Word in VBA using selection.previous wdword, 1 bug -


i'm trying write macro type previous word @ cursor. problem when i'm using "selection.previous wdword, 1" previous character, 2 previous characters , seems bug. when press "delete" button works , strange me. i'd glad if help. ultimate goal create calendar converter inside word using code. here how test it:

msgbox selection.previous(unit:=wdword, count:=1) 

it same using next :

msgbox selection.next(unit:=wdword, count:=1) 

instead of next word, returns word after!

for example text: during flight on 21/3/1389

if cursor right after 1389, msgbox selection.previous(1,1) show "/"; if cursor after space after 1389 shows "1389". problem is, think, space. question if there alternative read previous word instead of command (selection.previous(unit:=wdword, count:=1))

word not buggy - it's behaving designed. something has tell word words start , end. when cursor stands right of space it's (quite logically) @ beginning of next word. going 1 word going pick 1389 instead of /.

you can work around in code. i'm sure there's more 1 way it, following works me in quick test:

sub getprevword()   dim rngsel word.range, rngprev word.range    set rngsel = selection.range   set rngprev = rngsel.duplicate   rngprev.movestart wdcharacter, -1   if left(rngprev.text, 1) = " "     rngprev.collapse wdcollapsestart   end if   rngprev.select   msgbox selection.previous(unit:=wdword, count:=1)   rngsel.select end sub 

what it's doing using 2 ranges: 1 hold original selection, other work (rngprev). rngprev extended backwards 1 character , character evaluated. if it's space rngprev collapsed starting point. (think of pressing left arrow key of selection.) in case, rngprev selected , msgbox code run. finally, original range selected again.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -