Deleting a Range

G

Gordon Bentley-Mix

I have used variations of the following code snippets in several templates.

Sub DeleteMe()
If ActiveDocument.Bookmarks.Exists("DeleteMe") Then
ActiveDocument.Bookmarks("DeleteMe").Range.Delete
End Sub

Sub DeleteMyStyleParas()
Dim myPara As Paragraph
For Each myPara In ActiveDocument.Paragraphs
If myPara.Style = "myStyle" Then myPara.Range.Delete
Next myPara
End Sub

Without going into a great deal of detail, the basic idea behind this code
is to clean up a document after running a UserForm. The first deletes a bit
of the content surrounded by a bookmark, and the second removes all the
paragraphs of a certain style. Most of the time it works flawlessly, but in a
couple of templates I've notices some problems.

First, 'DeleteMe' doesn't delete correctly if the bookmark contains a table
(sometimes - see end of this paragraph). The content of the table is deleted
- although sometimes the formfields in the table remain - but the table
itself is left. This happens even if the bookmark includes the paragraph
before the table, which is the only way it works in the other templates where
it does work. And if it includes the paragraph after the table as well, it
deletes the table but leaves the leading and trailing paragraphs and the
bookmark behind. It does work if I select the bookmark range first and then
delete the selection, but I'm trying to get away from using the Selection
object as much as possible. And it works fine in the same template if the
bookmark only contains text or formfields (and in a couple of notable cases,
even when the bookmark contains a table - GAHHH!).

Second, 'DeleteMyStyleParas' doesn't delete either - at all. The paragraphs
with a style of 'MyStyle' still remain in the document. I've checked to make
sure the style name is correct and even tried using .NameLocal, but no joy.

These things happen only in the templates in which they happen and never in
those in which they don't happen. (Wow! That's convoluted, but I can't think
of a better way to explain it. ;-D) Any ideas?

--
Cheers!
Gordon
The Kiwi Koder

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
H

Helmut Weber

Hi Gordon,

try:
.... range.cut
instead of
.... range.delete

I have no idea what's wrong with
DeleteMyStyleParas.


--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
G

Gordon Bentley-Mix

Thanks Helmut. I thought of that, but: 1) I really don't want to stuff
something I don't need to into the paste buffer; and 2) .Delete works just
fine in other templates and even in other places in the same template. This
leads me to believe that it's not actually the code that's at fault. I was
just wondering if anyone else has run into a similar problem and had found a
solution.

I'm going to try a few other things today, and if all else fails I'll
rebuild the template from scratch and just import the code - not my first
choice naturally, but I don't know what else to do.
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
G

Gordon Bentley-Mix

Wouldn't you just bloody know it! Today it works fine. Must have been wearing
the wrong colour socks yesterday. GRRR!!! I *HATE* Word! If it didn't pay the
bills I wouldn't touch it ever again!
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 

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