Consulting

Results 1 to 2 of 2

Thread: Problems with Page X of Y numbers

  1. #1

    Problems with Page X of Y numbers

    Hi everyone,
    new to forum and word vba, and I very need help.
    I have to rearrange a doc file (composed by multiple sections) created by an application from a third party.
    I managed to resolve all the problems but pagenumbers in header, given in the form of "Page X of Y".
    I have to decide yet if maintaining the native page number format or change it.
    In the case I want to maintain the original format, my problem is that the Y is always of 1 minus than total page numbers (if I activate field codes, I get <NUMPAGES -1> and I can't manage to solve it with vba, that is a must).
    In the case I want to delete the native format "Page X of Y", I can remove "Page", "X" and "of", but not the same for "Y", which still remains: I've searched in a number of vba forums, but I found only the macro for deleting page numbers, that works for the "X" as I said, but not with the "Y"!
    These are the code I use for deleting "Page X of":

    Sub cancella()
    Dim objSect As Section
    Dim objHF As HeaderFooter
    Dim objPNum As PageNumber
    For Each objSect In ActiveDocument.Sections
    For Each objHF In objSect.Headers
    For Each objPNum In objHF.PageNumbers
    objPNum.Delete
    Next
    Next
    For Each objHF In objSect.Footers
    For Each objPNum In objHF.PageNumbers
    objPNum.Delete
    Next
    Next
    Next


    Call cancellare
    End Sub


    Sub cancellare()
    WordBasic.viewheaderonly
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
    .Text = "*" (the * is substitued by "Page" first and by"of" then, because if I use * as wildcard the
    macro deletes one char at time only):
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    End With
    If Selection.Find.Execute Then
    With Selection
    Selection.Delete
    End With
    End If
    End Sub


    Very thanks in advance for the precious help!!!

  2. #2
    Without knowing what else is in the header/footers or even if there is any additional text in the same paragraph as the page number it is a bit tricky to ensure that only the required field construction is deleted, however the following should work

    Option Explicit
    
    Sub Macro1()
    
    Dim objSect As Section
    Dim objHF As HeaderFooter
    Dim objField As Field
    Dim oRng As Range
        ActiveWindow.View.ShowFieldCodes = True
        For Each objSect In ActiveDocument.Sections
            For Each objHF In objSect.Headers
                If objHF.Exists Then
                    For Each objField In objHF.Range.Fields
                        If objField.Type = wdFieldPage Then
                            Set oRng = objField.Result
                            DelPageOf oRng
                            Exit For
                        End If
                    Next objField
                End If
            Next objHF
            For Each objHF In objSect.Footers
                If objHF.Exists Then
                    For Each objField In objHF.Range.Fields
                        If objField.Type = wdFieldPage Then
                            Set oRng = objField.Result
                            DelPageOf oRng
                            Exit For
                        End If
                    Next objField
                End If
            Next objHF
        Next objSect
        ActiveWindow.View.ShowFieldCodes = False
    lbl_Exit:
        Set objSect = Nothing
        Set objHF = Nothing
        Set objField = Nothing
        Set oRng = Nothing
        Exit Sub
    End Sub
    
    Private Sub DelPageOf(oRng As Range)
        oRng.End = oRng.Paragraphs(1).Range.End - 1
        If oRng.Fields.Count = 3 Then
            oRng.Fields(1).Delete
            oRng.Fields(2).Delete
            oRng.Fields(1).Delete
            oRng.Collapse 1
            Do While oRng.Characters(1) = Chr(32) Or _
               oRng.Characters(1) = "o" Or oRng.Characters(1) = "f"
                oRng.Characters(1).Delete
            Loop
        End If
    lbl_Exit:
        Set oRng = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word)
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •