Macro to delete last section of document

G

Grant Ord

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.
 
D

Dave Lett

Hi,

I think you can use the following:

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

HTH,
Dave
 
G

Grant Ord

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.
 
D

Dave Lett

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
 

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