View Single Post
 
Old 02-07-2013, 10:36 AM
kaurp kaurp is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Jan 2013
Posts: 3
kaurp is on a distinguished road
Default Restarting page numbering only if Section begins with Heading 1

Hi,

I have a macro that does various different things. I want it to not restart page numbering if the pages go from landscape to portrait, only if the new section begins with a heading 1. Since you need a section break when change from landscape to portrait, it's restarting the page numbering. I even tried to search if the page is landscape, but the restart starts at the next portrait page. Any help would be appreciated. I've attached my macro.

Code:
Public Sub setFootersToHeadings()
    Dim doc As Document
    Dim s As Section
    Dim h As HeaderFooter
    Dim p As Paragraph
    Dim page As page
    Dim ctr As Integer
    'ActiveDocument.TablesOfContents(1).Update
           
    Set doc = ActiveDocument
    ctr = 0
    Dim hFound As Boolean
    Dim footerLabel As String
    'footerLable = ""
    Dim tStr As String
           
    For Each s In doc.Sections

        For Each p In s.Range.Paragraphs
            If p.Style = ActiveDocument.Styles("Heading 1") Then
                footerLabel = Left(p.Range.Text, Len(p.Range.Text) - 1)
            End If
                  
            If p.Range.Font.Color = wdColorRed Then
                p.Range.Select
                p.Range.Font.Color = wdColorBlack
            End If
        Next p
               
        'you have a label, assign it to the section

        Set h = s.Footers(1)
        If h.Range.Text <> "Table of Contents" And footerLabel <> "" Then
                   
            h.LinkToPrevious = False
            'set the footer in the section
            'first clean out the old one
            'ctr = h.Range.Text
            tStr = h.Range.Text
              
            'set the footer to the label
            h.Range.Text = Replace(h.Range.Text, tStr, footerLabel)
                         
            With h.PageNumbers
                .Add PageNumberAlignment:=wdAlignPageNumberCenter
                .IncludeChapterNumber = True
                '.ChapterPageSeparator = wdSeparatorEnDash
                .RestartNumberingAtSection = True
                .StartingNumber = 1
            End With
        End If
            If s.PageSetup.Orientation = wdOrientLandscape Then
                
                h.LinkToPrevious = True
                h.PageNumbers.RestartNumberingAtSection = False
            End If

        
    Next s
    ActiveDocument.TablesOfContents(1).Update

    For Each s In doc.Sections
        For Each p In s.Range.Paragraphs
          If p.Style.NameLocal = "Heading 1" Then
                p.Range.Select
                p.Range.Font.Color = wdColorWhite
                p.Range.Font.Size = 5
            End If

            If p.Style.NameLocal = "Heading 2" Then
                p.Range.Select
                p.Range.Font.Color = wdColorWhite
                p.Range.Font.Size = 5
            End If
            If p.Style.NameLocal = "Heading 5" Then
                p.Range.Select
                p.Range.Font.Color = wdColorWhite
                p.Range.Font.Size = 5
            End If

        Next p
    Next s
    

    ActiveDocument.TablesOfContents(2).Update
    ActiveDocument.TablesOfContents(3).Update
         MsgBox ("Done Macro")
          
End Sub
Reply With Quote