Searching for section breaks that have got landscape orientation

Dear Experts:

Is it possible to search for only those section breaks that have got LANDSCAPE orientation, using VBA?

Help is appreciated. Thank you very much in advance.

Regards, Andreas
Andreas HermleTeam leaderAsked:
Who is Participating?
 
GrahamSkanConnect With a Mentor RetiredCommented:
OK Andreas. This sets the selection to the break that begins a landscaped section.

Sub Findsections()
Dim rng As Range
Dim sec As Section

    Set rng = Selection.Range
    rng.End = ActiveDocument.Range.End
    Do While rng.Find.Execute("^b")
        Set sec = rng.Sections(1)
        If ActiveDocument.Sections(sec.Index + 1).PageSetup.Orientation = wdOrientLandscape Then
            Exit Do
        End If
    Loop
    If rng.Find.Found Then
        rng.Select
        MsgBox "Found on page no: " & Selection.Information(wdActiveEndPageNumber)
    Else
        MsgBox "Not found"
    End If
End Sub

PS. Who's Greg? ;)
0
 
GrahamSkanRetiredCommented:
Hi Andreas.
A thunderstorm just gave us a power cut, so I've had to retype everthing. It was heading your way. You might see it tomorrow, so watch out for it.
As you know, there is only one search symbol for a section break, and we have to examine the sections on either side of the break to find if it's a 'Landscape' break or not.
Actually, it's not clear whether you need the break at the beginning or the one at the end of the section. It is the one at the end that carries the page setup information such as orientation. Try deleting one, and you will see that the new combined section has the formatting of the second.
0
 
GrahamSkanRetiredCommented:
Did my usual thing of forgetting to post the code:

Option Explicit

Sub Findsections()
Dim rng As Range
Dim sec As Section
Dim strOrientation1 As String
Dim strOrientation2 As String

    Set rng = ActiveDocument.Content
    Do While rng.Find.Execute("^b")
        Set sec = rng.Sections(1)
        strOrientation1 = GetOrientationWord(sec.PageSetup.Orientation)
        strOrientation2 = GetOrientationWord(ActiveDocument.Sections(sec.Index + 1).PageSetup.Orientation)
        MsgBox "Section break at " & rng.Start & vbCrLf & _
        "ends a " & strOrientation1 & " section and begins a " & strOrientation2 & " section"
    Loop
End Sub


Function GetOrientationWord(o As Integer) As String
    Select Case o
        Case wdOrientLandscape
            GetOrientationWord = "Landscape"
        Case wdOrientPortrait
            GetOrientationWord = "Portrait"
    End Select
End Function
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Andreas HermleTeam leaderAuthor Commented:
Greg, Looks very sophisticated as always and it seems to work although I am not quite sure what the message box says:
It says section break at 13676 and section break at a 136657. What do these figures stand for?
Actually I wanted the macro to jump to the nearest section break that has landscape orientation whenever I run the macro?
0
 
Andreas HermleTeam leaderAuthor Commented:
Great working as desired. Thank you very much.
As for Greg,  I mixed you up with sb.
0
 
GrahamSkanRetiredCommented:
No harm done. I was already mixed up, anyway. :>)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.