Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a word application. The header shows pagination in the style (1 of 9). How do I decrease the total number of pages programatically. I tried to record macro, but that doesn't work as expected.
Page {PAGE} of {={NUMPAGES}-1} I have to achieve this through vba code.

What I have tried:

THE RECORDED MACRO
<pre>Sub Macro3()
'
' Macro3 Macro
'
'
    If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
        ActiveWindow.Panes(2).Close
    End If
    If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
        ActivePane.View.Type = wdOutlineView Then
        ActiveWindow.ActivePane.View.Type = wdPrintView
    End If
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    Application.Templates( _
        "C:\Users\v-abhink\AppData\Roaming\Microsoft\Document Building Blocks\1033\16\Built-In Building Blocks.dotx" _
        ).BuildingBlockEntries("Bold Numbers 3").Insert Where:=Selection.Range, _
        RichText:=True
    ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
    Selection.TypeText Text:="="
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
        PreserveFormatting:=False
    Selection.TypeText Text:="NUMPAGES"
    Selection.MoveRight Unit:=wdCharacter, Count:=2
    Selection.TypeText Text:="-1"
    ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
End Sub
Posted
Updated 15-Jul-19 9:03am

You've gt 2 ways to achieve that:
method #1 PageNumbers.Add method (Word) | Microsoft Docs[^]
method #2 Fields.Add method (Word) | Microsoft Docs[^]

VB
Sub AddPageNumbering()
    Dim doc As Document
    Dim header As Range
    Set doc = ActiveDocument 
    Set header = doc.Sections(ActiveDocument.Sections.Count) _
        .Headers(wdHeaderFooterPrimary).Range
    With header
        .Paragraphs(1).Alignment = wdAlignParagraphCenter
        .TypeText Text:="Page "
        .Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
            "PAGE ", PreserveFormatting:=True
        .TypeText Text:=" of "
        .Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
            "NUMPAGES ", PreserveFormatting:=True
    End With
End Sub


For further details, please see:
How to use Word automation to count page number in each section of a document - Office | Microsoft Docs[^]
Fields object (Word) | Microsoft Docs[^]
 
Share this answer
 
Comments
dirtyshooter 13-Jul-19 1:29am    
This code will be helpful for adding pagination, I believe. But I need the total no. of pages to be decreased. If it is "Page 1 of 9", it should display "Page 1 of 8"
Maciej Los 13-Jul-19 15:54pm    
dirtyshooter 14-Jul-19 23:47pm    
Sorry i am new to this. How do I do that?
Maciej Los 15-Jul-19 16:38pm    
Do you mean subtract one?
result = PageNumbers.Count - 1
<pre>Sub InsertFooter()
    Dim rng As Range
    With ActiveDocument.Sections(1)
        With .Headers(wdHeaderFooterPrimary)
          Set rng = .Range.Duplicate
            rng.Collapse wdCollapseEnd
            rng.InsertBefore vbTab & "Page { PAGE } of { = { NUMPAGES } -1 }"
            TextToFields rng
        End With
    End With
End Sub
Sub TextToFields(rng1 As Range)
    Dim c As Range
    Dim fld As Field
    Dim f As Integer
    Dim rng2 As Range
    Dim lFldStarts() As Long
    
    Set rng2 = rng1.Duplicate
    rng1.Document.ActiveWindow.View.ShowFieldCodes = True
 
    For Each c In rng1.Characters
        DoEvents
        Select Case c.Text
            Case "{"
                ReDim Preserve lFldStarts(f)
                lFldStarts(f) = c.Start
                f = f + 1
            Case "}"
                f = f - 1
                If f = 0 Then
                    rng2.Start = lFldStarts(f)
                    rng2.End = c.End
                    rng2.Characters.Last.Delete '{
                    rng2.Characters.First.Delete '}
                    Set fld = rng2.Fields.Add(rng2, , , False)
                    Set rng2 = fld.Code
                    TextToFields fld.Code
                End If
            Case Else
        End Select
    Next c
    rng2.Expand wdStory
    rng2.Fields.Update
    rng1.Document.ActiveWindow.View.ShowFieldCodes = False
End Sub
VB

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900