Delete multiple sections with range object

P

Pam

I am using Word 2003 and I have a word document with 17 sections, and I
only want to delete section 3 through section 16, but the code below is
not working. I think it is the object I put after the section number.
Can anyone help?

Dim Range1 As Range
Set Range1 =
ActiveDocument.Range(Start:=ActiveDocument.Sections(2).Start,
End:=ActiveDocument.Sections(15).End)
With Range1
.Select
.Delete
EndWith

Thanks

Pam
 
G

Greg Maxey

Pam,

Seems it would have been simplier to select the text and delete ;-)

You left out ".Range" in your start/stop (variable, parameter,
whatever it is called)

Your also don't need the With statement or the .Select

Sub Test1()
Dim Range1 As Range
Set Range1 =
ActiveDocument.Range(ActiveDocument.Sections(2).Range.Start, _
ActiveDocument.Sections(4).Range.End)
Range1.Delete
End Sub
 
J

JCNZ

Hi Pam:

Try:

Sub Rdel()
With ActiveDocument
.Range(.Sections(3).Range.Start, .Sections(16).Range.End).Delete
End With
End Sub
 
P

Pam

Thanks it worked.

Now I am trying to delete sections 2 then 4-16. I have it deleting
section 2 but it skips 4 and deletes down to section 16.

I tried a goto command to have the cursor goto section 4, but it does
not seem to be working, any suggestions.

With ActiveDocument
.Sections(2).Range.Select
.Sections(2).Range.Delete
'wdGoToSection , Count:=4
.Range(.Sections(4).Range.Start, .Sections(16).Range.End).Delete
End With
End If

Thanks

Pam
 
J

Jean-Guy Marcil

Pam was telling us:
Pam nous racontait que :
Thanks it worked.

Now I am trying to delete sections 2 then 4-16. I have it deleting
section 2 but it skips 4 and deletes down to section 16.

I tried a goto command to have the cursor goto section 4, but it does
not seem to be working, any suggestions.

With ActiveDocument
.Sections(2).Range.Select
.Sections(2).Range.Delete
'wdGoToSection , Count:=4
.Range(.Sections(4).Range.Start, .Sections(16).Range.End).Delete
End With
End If

You do not need to select something to delete it.

Avoid the Selection object.

Now, one you delete section 2, the old section 4 is now section 3...

When doing multiple deletions like that, it is easier to do it in reverse,
otherwise you have to take into account the fact that your deletions change
the index numbers.

Try (UnTested):

With ActiveDocument
.Range(.Sections(4).Range.Start, .Sections(16).Range.End).Delete
.Sections(2).Range.Delete
End With

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

JCNZ

It's a pity that you can't name sections. If you could, you could write:

Activedocument.Sections("Fred").Range.Delete

- and you wouldn't have to worry about section numbers changing. However, if
you put a bookmark in a section you can write:

Activedocument.Bookmarks("bmkS2").Range.Sections(1).Range.Delete

- and that will delete the section regardless of its number.
 
G

Greg Maxey

JCNZ,

How is that an improvement over clicking in the selection and:

Selection.Sections(1).Range.Delete

I mean if you bookmark your sections with bmkS1, bmkS2, bmkS3 etc. then
delete section two using your suggestion the section will be deleted
along with the bookmark. The old section three will become the new
section 2 and contain a bookmark named bkmS3. If you want to delete
section 2 and run your code it will throw and error.

While I appreciate your thoughts on "named" bookmarks, I don't see the
benefit of your proposed solution.

Perhaps I am missing something.
 
J

Jean-Guy Marcil

Greg Maxey was telling us:
Greg Maxey nous racontait que :
JCNZ,

How is that an improvement over clicking in the selection and:

Selection.Sections(1).Range.Delete

I mean if you bookmark your sections with bmkS1, bmkS2, bmkS3 etc.
then delete section two using your suggestion the section will be
deleted along with the bookmark. The old section three will become
the new section 2 and contain a bookmark named bkmS3. If you want to
delete section 2 and run your code it will throw and error.

While I appreciate your thoughts on "named" bookmarks, I don't see the
benefit of your proposed solution.

Perhaps I am missing something.

Not only that, even more of an issue is that unless you are working with a
protected document, you have to rely on the user not to delete the bookmark
while editing the document...
Never trust a user to not screw something up that can be screwed up....
Murphy's law definitely applies!

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

JCNZ

Greg:

The bookmarks offer an advantage if you want to delete a number of specific
sections at some point in a document's life. They might, for example, contain
explanatory text which wouldn't be required in a final document. Way back,
Pam was '... trying to delete sections 2 then 4-16.' - so it looks as if she
had something like this in mind. You wouldn't have to know the numbers of the
sections, and it wouldn't matter if the user had inserted some sections.

My 'bmkS1', 'bmkS2' and so on weren't intended to suggest that they had to
be in specifically numbered sections. They could equally well have been
'bmkIntro', 'bmkClause123' and so on.

I assume that the programmer would add code to check for the existence of
the relevant bookmarks before the deletion code ran. If the bookmark isn't
there, the code must already have run and it wouldn't be appropriate to run
it again.
 
G

Greg Maxey

Ok, I see what you mean now. That looks to me like a fine and workable
solution to the scenario that you described.
 

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