Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-07-2013, 10:36 AM
kaurp kaurp is offline Restarting page numbering only if Section begins with Heading 1 Windows 7 64bit Restarting page numbering only if Section begins with Heading 1 Office 2010 64bit
Novice
Restarting page numbering only if Section begins with Heading 1
 
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
  #2  
Old 02-08-2013, 03:50 AM
macropod's Avatar
macropod macropod is offline Restarting page numbering only if Section begins with Heading 1 Windows 7 64bit Restarting page numbering only if Section begins with Heading 1 Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

What you'd need to do is to insert a Section break before the heading (only if one isn't already there), then unlink the Section header/footer from the previous Section before er-setting the page numbering.

PS: Your code incorporates numberous '.Select' directives that add nothing to what you're doing other than slowing things down.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 02-08-2013, 07:05 AM
kaurp kaurp is offline Restarting page numbering only if Section begins with Heading 1 Windows 7 64bit Restarting page numbering only if Section begins with Heading 1 Office 2010 64bit
Novice
Restarting page numbering only if Section begins with Heading 1
 
Join Date: Jan 2013
Posts: 3
kaurp is on a distinguished road
Default

The problem with that is that this is a 500 page document that is automatically generated. That's why I was trying to find a way to do it within the macro.

Thank you for the Select tip. I didn't realize that
Reply With Quote
  #4  
Old 02-08-2013, 04:20 PM
macropod's Avatar
macropod macropod is offline Restarting page numbering only if Section begins with Heading 1 Windows 7 64bit Restarting page numbering only if Section begins with Heading 1 Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try the following:
Code:
Sub ProcessH1Breaks()
Application.ScreenUpdating = False
Dim Rng1 As Range, Rng2 As Range, Sctn As Section, HdFt As HeaderFooter, i As Long
Set Rng1 = ActiveDocument.Range(0)
Set Rng2 = ActiveDocument.Range(0)
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    'Find all Section breaks
    .Text = "^b"
    .Replacement.Text = "^&"
    'Makes sure the Section break's Style = Normal
    .Replacement.Style = "Normal"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
    'Find all "Heading 1" text
    .Text = ""
    .Style = "Heading 1"
    .Wrap = wdFindStop
    .Execute
  End With
  Do While .Find.Found
    Rng1.End = .Duplicate.Start + 1
    Rng2.End = .Duplicate.Start - 1
    'Make sure there is a Next Page Section break before the "Heading 1" paragraph
    If Rng1.Sections.Count = Rng2.Sections.Count Then
      Set Sctn = ActiveDocument.Sections.Add(.Duplicate.Characters.First, wdSectionNewPage)
    Else
      Set Sctn = .Duplicate.Sections.First
    End If
    'Restart the Section's page numbering
    For Each HdFt In Sctn.Headers
      With HdFt
        .LinkToPrevious = False
        .PageNumbers.RestartNumberingAtSection = True
        .PageNumbers.StartingNumber = 1
      End With
    Next
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
  'Repeat making the Sections break Style = Normal
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "^b"
    .Replacement.Text = "^&"
    .Replacement.Style = "Normal"
    .Forward = True
    .Wrap = wdFindContinue
    .Execute Replace:=wdReplaceAll
  End With
  'Make sure all changes in page orientation continue page numbering,
  'PROVIDED the Section doesn't start with Heading 1.
  For i = 2 To .Sections.Count
    If .Sections(i).PageSetup.Orientation <> .Sections(i - 1).PageSetup.Orientation Then
      If .Sections(i).Range.Paragraphs(1).Style <> "Heading 1" Then
        For Each HdFt In .Sections(i).Headers
          With HdFt
            .LinkToPrevious = True
          End With
        Next
      End If
    End If
  Next
End With
Set Rng1 = Nothing: Set Rng2 = Nothing: Set Sctn = Nothing
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 11-02-2017, 01:22 AM
arielina arielina is offline Restarting page numbering only if Section begins with Heading 1 Windows XP Restarting page numbering only if Section begins with Heading 1 Office 2010 64bit
Novice
 
Join Date: Nov 2017
Posts: 1
arielina is on a distinguished road
Default

Hi
I have a similar task and the provided solution works wonderfully for me. However, I would also need the footer to show the total page number for each Heading 1.
Can anyone please show me how?
Thanks
Reply With Quote
  #6  
Old 11-02-2017, 04:36 AM
macropod's Avatar
macropod macropod is offline Restarting page numbering only if Section begins with Heading 1 Windows 7 64bit Restarting page numbering only if Section begins with Heading 1 Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Word has no means of doing that unless you insert Section breaks either side of your Heading 1 ranges and insert a SECTIONPAGES field in the footer for each of those Sections. The macro code inserts a Section break before each Heading 1, which may suffice, but I really have no idea what your interpretation of a Heading 1 range is; for all I know, it might end at the next Heading 1 (in which case all you need do is insert the SECTIONPAGES field in the footer) or somewhere before then (in which case more work is needed).
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Restarting page numbering only if Section begins with Heading 1 Page numbering with Section 1.1 Page 1 sunalta Word 1 05-31-2012 02:48 PM
Restarting page numbering only if Section begins with Heading 1 Page numbering starting on 2 after section break pamm13 Word 1 06-22-2011 11:10 AM
Heading numbering and section breaks uby Word 2 11-22-2010 10:03 PM
Page number picking up heading numbering?! Ulodesk Word 0 09-24-2009 01:56 PM
Section breaks, footers and page numbering yeswab Word 0 03-19-2009 06:37 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:05 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft