Place cursor at the end of a section

P

Peter Faulhaber

Hi All,

I have a document with multiple sections. The first page is a TOC and the
last one is the INDEX.

The user can add sections in the document. VBA code is working fine. The VBA
code is searching for the end of section and add a section break

BUT when a user want to add a section after INDEX, the section is added
after the TOC?

The VBA code is
Sub Sectie_Einde()
' Opzoek naar het einde van de sectie

Selection.Find.ClearFormatting
With Selection.Find
.Text = "^b"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.CorrectHangulEndings = True
End With

Selection.Find.Execute

Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeParagraph
Selection.InsertBreak Type:=wdSectionBreakNextPage

End Sub
 
J

Jezebel

Forget all that nonsense. All you need is --

ActiveDocument.Sections(ActiveDocument.Sections.Count - 1).Range.InsertBreak
 
G

Greg Maxey

Jezebel,

Wouldn't he need a bit more than that? First that errors if there is
only one section. If there are two sections it deletes one and puts in
a page break.

Perhaps:

Sub Test()
With Selection.Sections(1).Range
.Collapse wdCollapseEnd
.InsertBreak wdSectionBreakNextPage
End With
End Sub
 
D

Dave Lett

Hi Peter,

Maybe this will be useful:

Dim iSec As Integer
Dim oRng As Range
Set oRng = Selection.Range
oRng.Start = ActiveDocument.Range.Start
iSec = oRng.Sections.Count

If iSec = ActiveDocument.Sections.Count Then
MsgBox "You cannot add a section after " & _
"the index. Place the cursor in a section " & _
"that comes before the index and run the " & _
"routine again.", vbInformation
End
End If

Selection.EndOf _
Unit:=wdSection, _
Extend:=wdMove
Selection.InsertBreak Type:=wdSectionBreakNextPage
Selection.TypeText Text:="New Section"


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