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 / October 2006

Tip: Looking for answers? Try searching our database.

Delete multiple sections with range object

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Pam - 20 Oct 2006 17:49 GMT
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
Greg Maxey - 20 Oct 2006 17:59 GMT
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

> 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
[quoted text clipped - 13 lines]
>
> Pam
JCNZ - 21 Oct 2006 01:53 GMT
Hi Pam:

Try:

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

> 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
[quoted text clipped - 13 lines]
>
> Pam
Pam - 24 Oct 2006 13:22 GMT
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

> Hi Pam:
>
[quoted text clipped - 23 lines]
> >
> > Pam
Jean-Guy Marcil - 24 Oct 2006 17:59 GMT
Pam was telling us:
Pam nous racontait que :

> Thanks it worked.
>
[quoted text clipped - 11 lines]
> 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

Signature

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

JCNZ - 24 Oct 2006 20:55 GMT
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.

> Pam was telling us:
> Pam nous racontait que :
[quoted text clipped - 31 lines]
>     .Sections(2).Range.Delete
> End With
Greg Maxey - 24 Oct 2006 21:06 GMT
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.

> It's a pity that you can't name sections. If you could, you could write:
>
[quoted text clipped - 49 lines]
> > jmarcilREMOVE@CAPSsympatico.caTHISTOO
> > Word MVP site: http://www.word.mvps.org 
Jean-Guy Marcil - 25 Oct 2006 05:59 GMT
Greg Maxey was telling us:
Greg Maxey nous racontait que :

> JCNZ,
>
[quoted text clipped - 12 lines]
>
> 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!

Signature

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

JCNZ - 29 Oct 2006 05:41 GMT
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.

> JCNZ,
>
[quoted text clipped - 66 lines]
> > > jmarcilREMOVE@CAPSsympatico.caTHISTOO
> > > Word MVP site: http://www.word.mvps.org 
Greg Maxey - 29 Oct 2006 05:49 GMT
Ok, I see what you mean now.  That looks to me like a fine and workable
solution to the scenario that you described.

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Greg:
>
[quoted text clipped - 87 lines]
>>>> jmarcilREMOVE@CAPSsympatico.caTHISTOO
>>>> Word MVP site: http://www.word.mvps.org
 
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.