Loveb
Loveb

Reputation: 335

Change selected sections

enter image description hereMy problem is that depenting on in which direction I select the text, from page 5-10 or from page 10-5, I get different results. The code below starts from the active page.

Sub setOdd()
Call editBrake("odd")
End Sub

Sub editBrake(typ As String)
Dim iSec As Long, iSecs As Long, iSecTot As Long, i As Long, s As Section

iSec = Selection.Information(wdActiveEndSectionNumber)
iSecs = iSec + Selection.Sections.Count - 1
iSecTot = ActiveDocument.Sections.Count

If iSecTot >= iSec Then
    For i = iSec To iSecs
        Set s = ActiveDocument.Sections(i)
        If typ = "new" Then
            s.PageSetup.SectionStart = wdSectionNewPage
        ElseIf typ = "odd" Then
            s.PageSetup.SectionStart = wdSectionOddPage
        ElseIf typ = "even" Then
            s.PageSetup.SectionStart = wdSectionEvenPage
        End If
    Next i
ElseIf iSecTot = 1 Then
    MsgBox "There is no section breaks in document", vbInformation, "Change section breaks"
End If
End Sub

Upvotes: 0

Views: 210

Answers (2)

Loveb
Loveb

Reputation: 335

Thanks to Timothy Rylatt the following code works. (I also have a code for Selection.Sections.Count = 1)

Sub editBrake(typ As String)
Dim s As Section
Dim i As Integer
Dim skipFirst As Boolean
i = 1
If Selection.Sections.Count > 1 Then
    skipFirst = True
    For Each s In Selection.Sections
        If skipFirst Then
            skipFirst = False
        Else
            If typ = "new" Then
                s.PageSetup.SectionStart = wdSectionNewPage
            ElseIf typ = "odd" Then
                s.PageSetup.SectionStart = wdSectionOddPage
            ElseIf typ = "even" Then
                s.PageSetup.SectionStart = wdSectionEvenPage
            End If
        End If
        i = i + 1
    Next
Else
    MsgBox "There are no section breaks in document", vbInformation, "Change section breaks"
End If
End Sub

Upvotes: 0

Timothy Rylatt
Timothy Rylatt

Reputation: 7850

Just keep it simple by looping through the selected sections.

Sub editBreak(typ As String)
    Dim s As Section

    If ActiveDocument.Sections.Count > 1 Then
        For Each s In Selection.Sections
            If typ = "new" Then
                s.PageSetup.SectionStart = wdSectionNewPage
            ElseIf typ = "odd" Then
                s.PageSetup.SectionStart = wdSectionOddPage
            ElseIf typ = "even" Then
                s.PageSetup.SectionStart = wdSectionEvenPage
            End If
        Next
    Else
        MsgBox "There are no section breaks in document", vbInformation, "Change section breaks"
    End If
End Sub

Upvotes: 1

Related Questions