Replace selection.range Word bug?

F

FK

Hi,

For several years I've coped with this problem, but now I really want (need)
a solution. I hope any of you can help me out.

What's the case, well in Word XP (and 97 and 2000), if I try to change the
contents of a selected range in a table in a frame, it goes wrong. I've
tried to replace the text directly, and tried first emptying the selection.
In the text is for example '[test]' selected.

- When I use selection.range.text="" then Word leaves 't]' while it
shouldn't.

- When I use selection.range.delete, surrounding spaces are also deleted. If
I put a space in front and in back of '[test]' then it leaves just the one
space instead of two.

- When I use selection.typetext text:="replacement", the spaces also get
messed up.

The ever so strange thing is that it doesn't go wrong in a normal text, or
in a normal table (not in a frame). When I say frame I mean a to a frame
converted textbox (Or a table created in Word 2003 which is always a table
within a frame?)

Extra info:

I search for 'codes' in the text and based on the contents of the 'codes' I
replace them with a certain text. So I cannot use the standard Find/Replace
functions. And somehwat I do use it, just a little split up, find first and
do something and replace.

TIA!
 
K

Klaus Linke

Hi Ronald,

Two reasons why things might go wrong:

-- The Range of a table cell has two formatting characters (end-of-cell-marker = CHr(13) & Chr(7)) at the end.
If you aren't aware of that, you might go wrong by two characters.
(If I use Selection.Range.Text = "" on a cell it always deletes the text, no matter whether the whole cell or only the text was selected. But maybe your code is a bit more complicated?)

-- Look at your "Smart cut and paste" options in "Tools > Options > Edit". They explain why spaces you didn't select sometimes get deleted, or spaces get inserted when you insert text. The same should happen in the user interface (Del or Backspace key) with your settings.

If you like that in the user interface, but don't like it in your macros, you can temporarily change the settings.

Dim oldPasteAdjustWordSpacing As Boolean
Options.PasteAdjustWordSpacing = False
' ... your code ...
Options.PasteAdjustWordSpacing = oldPasteAdjustWordSpacing

Some other options might also apply... If you keep having problems, you might check them out, or check if the same thing that goes "wrong" in your macro also happens in the user interface.
Say, Selection.Delete will delete the character to the right of an insertion point (nothing selected) ... something you might not expect, but the same as happens with the Del key.

Greetings,
Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top