Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / April 2007

Tip: Looking for answers? Try searching our database.

Weird behavior with Range.Delete

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mccaskey - 28 Apr 2007 21:59 GMT
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.
Jean-Guy Marcil - 29 Apr 2007 03:16 GMT
mccaskey was telling us:
mccaskey nous racontait que :

> This is too weird.
>
[quoted text clipped - 24 lines]
> 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 = ""

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

mccaskey - 29 Apr 2007 03:44 GMT
> mccaskey was telling us:
> mccaskey nous racontait que :
[quoted text clipped - 49 lines]
> jmarcilREM...@CAPSsympatico.caTHISTOO
> 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
Tony Jollans - 29 Apr 2007 21:34 GMT
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).

Signature

Enjoy,

Tony Jollans
Microsoft Word MVP

> mccaskey was telling us:
> mccaskey nous racontait que :
[quoted text clipped - 41 lines]
>    Set a = ActiveDocument.Range(Start:=1, End:=2)
>    a.Text = ""
Lene Fredborg - 29 Apr 2007 23:42 GMT
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…) .

Signature

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

> This is too weird.
>
[quoted text clipped - 24 lines]
> Can anyone else reproduce this? I've tried it with two releases of
> Word 2003.
Tony Jollans - 29 Apr 2007 23:59 GMT
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!

Signature

Enjoy,

Tony Jollans
Microsoft Word MVP

>I tested the macro yesterday and found that it always deleted the text
> (regardless of ! etc.). However, I now remember that I have previously
[quoted text clipped - 61 lines]
>> Can anyone else reproduce this? I've tried it with two releases of
>> Word 2003.
Jean-Guy Marcil - 30 Apr 2007 02:36 GMT
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
[quoted text clipped - 28 lines]
> 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!

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org


Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.