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

Tip: Looking for answers? Try searching our database.

Macro to delete last section of document

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Grant Ord - 01 Feb 2006 18:25 GMT
I'm having terrible trouble trying to write a macro that will delete the last
section in a document including all the content in that section.

I'm not very experienced at all in macros but I have created a routine in an
ASP page wich is based on a Word Macro that I recorded.

It opens a Word document and adds a new section at the end of it. In that
section it add hyperlinks. There may be between 1 and 20 hyperlinks and the
text and URL could be anything.

This works fine.

However, I need to create a companion macro which will remove any hyperlinks
I have added and also the section that I created.
What the documents will all have in common is a section break (which will be
either the only one or the last one). That section break and everything after
it is what I need to delete.

This seems quite simple but I just don't seem to be able to get the section
and further content selected.

Help would be appreciated.
Dave Lett - 01 Feb 2006 18:49 GMT
Hi,

I think you can use the following:

With ActiveDocument
   .Sections(.Sections.Count).Range.Delete
End With

HTH,
Dave

> I'm having terrible trouble trying to write a macro that will delete the last
> section in a document including all the content in that section.
[quoted text clipped - 18 lines]
>
> Help would be appreciated.
Grant Ord - 01 Feb 2006 18:57 GMT
Hey, thanks Dave.

Unfortuneately that deleted the entire contents of the document!

Would that be because there is only one section in the document, which
starts near the end?

I'm using a section just as a device to identify the part of the document
beyong which I want to delete.

Is there a similar syntax for deleting the last page? I could use a page
break instead of a section break.

> Hi,
>
[quoted text clipped - 29 lines]
> >
> > Help would be appreciated.
Dave Lett - 01 Feb 2006 19:21 GMT
Yes, it deletes the contents of the document if you only have one section. If
what you're really looking for is delete the LAST page, then you can use the
following:

Selection.EndKey Unit:=wdStory, Extend:=wdMove
ActiveDocument.Bookmarks("\Page").Range.Delete

This will leave and empty paragraph mark on the last page.
To remove it, add this to the end of the routine
Selection.TypeBackspace

HTH,
Dave

> Hey, thanks Dave.
>
[quoted text clipped - 42 lines]
> > >
> > > Help would be appreciated.
Grant Ord - 02 Feb 2006 10:08 GMT
Dave

Thanks very much for your help here.
That's worked just as you said.

> Yes, it deletes the contents of the document if you only have one section. If
> what you're really looking for is delete the LAST page, then you can use the
[quoted text clipped - 56 lines]
> > > >
> > > > Help would be appreciated.
 
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.