vba - Modifying a table cell's .Range.Text removes any attached comments -
i've written small vba program remove trailing whitespace ms word table cells. iterates through cells of each table , modifies .range.text
object using command
c.range.text = myre.replace(c.range.text, "")
where myre
vbscript regexp 5.5 object , myre.pattern = "\s+(?!.*\w)"
. entire program can found here.
the program works fine except 1 problem. removes comments cells well. before:
after (the space gone, comment):
looking @ local object tree, can see changing c.range.text
changes c.range.comments
- why?
what can prevent this?
when work range.text, case whenever regex or, indeed, function manipulates strings used, formatting , other non-text characters lost when pure string written cell.
for example, if single character in cell text formatted bold, bold formatting lost. or if change tracking in cell - lost. footnote or endnote lost. comments fall same category.
you need different approach, 1 respects how word stores non-text information in document. here's suggestion loops cells in table, picks range @ end of cell, moves starting point of range long pre-defined whitespace character found. when criterium no longer met, range deleted. (note don't know why needed use range.delete twice - first time had no effect.)
you need work out "whitespace". used space, carriage return , tab character. can add others swhitespace string.
sub removewhitespaceendofcell() dim cel word.cell dim swhitespace string dim rng word.range dim lwhitespacechars long 'define constitutes whitespace. 'here: space, carriage return , tab swhitespace = chr(32) & chr(13) & chr(9) each cel in activedocument.tables(1).range.cells set rng = cel.range 'set range end of cell rng.collapse wdcollapseend rng.moveend wdcharacter, -1 'move starting point long whitespace found lwhitespacechars = rng.movestartwhile(swhitespace, wdbackward) 'only if whitespace found, delete range if lwhitespacechars <> 0 'rng.select 'for debugging purposes rng.delete rng.delete end if next end sub
Comments
Post a Comment