Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

LinkBack Thread Tools Display Modes
Old 06-13-2018, 09:14 AM
Valvolino78 Valvolino78 is offline Windows 10 Office 2010 64bit
Join Date: Jun 2018
Posts: 1
Valvolino78 is on a distinguished road
Default 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

For Each objHF In objSect.Footers
For Each objPNum In objHF.PageNumbers

Call cancellare
End Sub

Sub cancellare()
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
End With
End If
End Sub

Very thanks in advance for the precious help!!!
Reply With Quote
Old 06-13-2018, 10:15 AM
d4okeefe d4okeefe is offline Windows 10 Office 2016
Advanced Beginner
Join Date: Apr 2013
Posts: 72
d4okeefe is on a distinguished road

I think that the "X" and "Y" in "Page X of Y" are fields. You can see fields in the document by typing Alt-F9. (Type Alt-F9 again to hide them.)

You can delete fields in the headers with a for loop.

Sub delete_fields_in_header()
    Dim x As Integer, num_fields As Integer
    num_fields = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Fields.Count
    For x = num_fields To 1 Step -1
    Next x
End Sub
Reply With Quote

page x of y

Thread Tools
Display Modes

Similar Threads
Thread Thread Starter Forum Replies Last Post
Footer for page numbers recently started showing a colored field when adding page numbers thefonebug Word 12 10-24-2016 05:18 AM
How do I edit my TOC to reference the section numbers instead of the page numbers??? mikey386 Word 0 12-17-2014 02:34 PM
How do I refer to page numbers, when the numbers change as I prepare the document? StevenD Word 5 11-29-2012 12:52 AM
Parallel page numbering/same page numbers for odd and even pages goran Word 2 11-02-2012 03:42 PM
Page Numbers Not Matching Chapter Numbers gracie5290 Word 1 02-02-2012 11:41 PM

All times are GMT -7. The time now is 07:40 AM.

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc. is not affiliated with Microsoft