Deleting the Last Section

S

Scott A

I'm trying to provide writers with a macro for deleting
the last (portrait) section of a document (in situations
where they decide that they want to end the document with
a landscape section). My users are not very good with
page formatting, so I'm trying to make it as easy as
possible.

I've started with a recorded macro that goes into the
header/footer of the last section, makes them same as
previous, reformats the page to landscape layout, and then
deletes the final section and the previous section break.

The recorded macro is not toggling properly between the
header and footer, and is not working too well... I'm
wondering if anyone has recommendations for reworking the
code.

Here's what I've got so far. I've manually modified the
code in the section marked (*), which is completely
incorrect:

========================================================

Sub DeleteLast()
'
' DeleteLast Macro
' Macro recorded 18.11.2003 by scott.adams
'
'Go to last page and set view
Selection.EndKey Unit:=wdStory
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or
ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If

'(*)Reset headers and footers to same as previous
With Section.Headers
HeaderFooter.LinkToPrevious = True
End With
With Section.Footer
HeaderFooter.LinkToPrevious = True

'Return to main document and set last section to
landscape
ActiveWindow.ActivePane.View.SeekView =
wdSeekMainDocument
Application.Browser.Target = wdBrowseSection
Application.Browser.Next
With Selection.PageSetup
.Orientation = wdOrientLandscape
End With

'Delete the last section
With ActiveDocument.Sections.Last.Range
.Delete
End With

'Remove the previous section break
Selection.TypeBackspace
Selection.Delete Unit:=wdCharacter, Count:=1

'Reset browse by object and return to top
Application.Browser.Target = wdBrowsePage
Selection.HomeKey Unit:=wdStory

End Sub

========================================================
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

Hi Scott,

Use the following code:

Dim myrange As Range
With ActiveDocument.Sections.Last
.Headers(wdHeaderFooterPrimary).LinkToPrevious = True
.Footers(wdHeaderFooterPrimary).LinkToPrevious = True
.PageSetup.Orientation = wdOrientLandscape
End With
ActiveDocument.Sections.Last.Range.Delete
Set myrange = ActiveDocument.Range
myrange.Start = myrange.End - 2
myrange.End = myrange.End - 1
myrange.Delete

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 

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