Weird behavior with Range.Delete

M

mccaskey

This is too weird.

Open a new word document. Enter a few characters.

Open VB and enter this subroutine:

Sub deleteChar()
Dim a as Range
Set a = ActiveDocument.Range(Start:=1, End:=2)
a.Delete
End Sub

Run the routine. As expected, the second character of your string will
be deleted . . .

.. . . unless the first character is ! and the second is a space, in
which case nothing happens.

If the first character is ! and the second is not a space, the routine
will replace your second character with a space.

The same happens if the first character is a right double quotation
mark (Alt + 0148).

What's going on?

Can anyone else reproduce this? I've tried it with two releases of
Word 2003.
 
J

Jean-Guy Marcil

mccaskey was telling us:
mccaskey nous racontait que :
This is too weird.

Open a new word document. Enter a few characters.

Open VB and enter this subroutine:

Sub deleteChar()
Dim a as Range
Set a = ActiveDocument.Range(Start:=1, End:=2)
a.Delete
End Sub

Run the routine. As expected, the second character of your string will
be deleted . . .

. . . unless the first character is ! and the second is a space, in
which case nothing happens.

If the first character is ! and the second is not a space, the routine
will replace your second character with a space.

The same happens if the first character is a right double quotation
mark (Alt + 0148).

What's going on?

Can anyone else reproduce this? I've tried it with two releases of
Word 2003.

Yeah, I see it too.

Maybe it has to do with the fact that Word sometimes interprets some
characters as "special" and makes them impossible to delete through VBA...
like the ¶ immediately preceding a table which can be problematic. In fact,
you will see this behaviour with any punctuation mark, not just the "!". I
guess Word sees the space following a punctuation mark as an essential
boundary between words and makes it difficult to delete...
In your case, it is easily fixed with:

Dim a As Range
Set a = ActiveDocument.Range(Start:=1, End:=2)
a.Text = ""



--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
M

mccaskey

mccaskey was telling us:
mccaskey nous racontait que :













Yeah, I see it too.

Maybe it has to do with the fact that Word sometimes interprets some
characters as "special" and makes them impossible to delete through VBA...
like the ¶ immediately preceding a table which can be problematic. In fact,
you will see this behaviour with any punctuation mark, not just the "!". I
guess Word sees the space following a punctuation mark as an essential
boundary between words and makes it difficult to delete...
In your case, it is easily fixed with:

Dim a As Range
Set a = ActiveDocument.Range(Start:=1, End:=2)
a.Text = ""

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site:http://www.word.mvps.org

Yes, I see it happens with ? also.

Thanks for the solution! I should have thought of that . . .

John
 
T

Tony Jollans

It looks as though the delete does happen - after it, the range starts at 1
and ends at 1 and its Text is "", but Word decides it is necessary to insert
a new space in the document (but not in the range).

The behaviour is also in the UI. Select the characters you would set the
Range to and press Delete and the space appears (or reappears).
 
L

Lene Fredborg

I tested the macro yesterday and found that it always deleted the text
(regardless of ! etc.). However, I now remember that I have previously
experienced related problems with spaces being inserted and deletion not
working. I then found that the problems were caused by the "Smart cut and
paste" option in Tools > Options > Edit tab being _turned on_. As soon as
that option was turned off, everything worked as expected. Normally, I have
the "Smart cut and paste" option turned off because of such problems. I just
tested the macro again with "Smart cut and paste" _turned on_ and the problem
described by the OP appeared.

In VBA, I have previously handled the problem by turning off "Smart cut and
paste" before executing operations that could else fail. I have saved the
user's original setting of the option and reapplied it afterwards. The code
lines for this could be:

Before the operation:
Dim OrigSmartCutAndPaste As Boolean
Options.SmartCutPaste = False

After the operation:
Options.SmartCutPaste = OrigSmartCutAndPaste

NOTE: I have found that there are two properties that can be used to turn
off the "Smart cut and paste" option: "SmartCutPaste" and
"PasteSmartCutPaste". I have used "SmartCutPaste" because I found that
"PasteSmartCutPaste" does not exist in Word 2000 (and earlier I think).

In general, my experience is the all options with the word "smart" in their
names are causing more trouble than most other options - not least in
relation to VBA (which I do not find smart…) .

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
T

Tony Jollans

Thank you Lene. I did look at a few options to see if any might affect it
but didn't try that one.
In general, my experience is the all options with the word "smart" in
their
names are causing more trouble than most other options

LOL! Sounds like a good thing to assume!
 
J

Jean-Guy Marcil

Lene Fredborg was telling us:
Lene Fredborg nous racontait que :
I tested the macro yesterday and found that it always deleted the text
(regardless of ! etc.). However, I now remember that I have previously
experienced related problems with spaces being inserted and deletion
not working. I then found that the problems were caused by the "Smart
cut and paste" option in Tools > Options > Edit tab being _turned
on_. As soon as that option was turned off, everything worked as
expected. Normally, I have the "Smart cut and paste" option turned
off because of such problems. I just tested the macro again with
"Smart cut and paste" _turned on_ and the problem described by the OP
appeared.

In VBA, I have previously handled the problem by turning off "Smart
cut and paste" before executing operations that could else fail. I
have saved the user's original setting of the option and reapplied it
afterwards. The code lines for this could be:

Before the operation:
Dim OrigSmartCutAndPaste As Boolean
Options.SmartCutPaste = False

After the operation:
Options.SmartCutPaste = OrigSmartCutAndPaste

NOTE: I have found that there are two properties that can be used to
turn off the "Smart cut and paste" option: "SmartCutPaste" and
"PasteSmartCutPaste". I have used "SmartCutPaste" because I found that
"PasteSmartCutPaste" does not exist in Word 2000 (and earlier I
think).

In general, my experience is the all options with the word "smart" in
their names are causing more trouble than most other options - not
least in relation to VBA (which I do not find smart.) .

Good catch..

The problem is that these options are generally turned on by default... So,
when you code for unknown users, I guess you have to do something like you
suggests... Turn them off, and then back to their original setting.

Thanks!

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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