#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
|
|||
|
|||
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. Code:
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 ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Fields(x).Delete Next x End Sub |
Tags |
page x of y |
|
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 |