Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-18-2024, 02:49 AM
Bikram Bikram is offline Setting page numbers Windows 10 Setting page numbers Office 2007
Advanced Beginner
Setting page numbers
 
Join Date: Jul 2021
Location: Nepal
Posts: 94
Bikram is on a distinguished road
Default Setting page numbers

Hello, Greeting to Everyone

While setting/adjusting the pagenumbers via VBA or pywin (python) the first page header gets expand which distrupts the formatting of every other pages.



Here is the code recorded from word

Code:
Sub AdjustpageNumbers()

    With Selection.Sections(1).Headers(1).PageNumbers
        .NumberStyle = wdPageNumberStyleArabic
        .HeadingLevelForChapter = 0
        .IncludeChapterNumber = False
        .ChapterPageSeparator = wdSeparatorHyphen
        .RestartNumberingAtSection = True
        .StartingNumber = 84
    End With
End Sub
If we adjust the pagenumber manually the problem doesn't exist. But if we attempt it with code the first page header will be expanded and a paragraph will appear. Is there a way to change the page numbers without causing expansion of Header in the first page?

I have attached the snipped images of before and after running the code. Please have a look.

first page does not have a page number (header) because the document is set to
Different First page = True
and
Different odd and even page = True

The main concern is that I have defined some functions that continue page numbers in multiple documents. But in some cases, the function returns wrong page numbers because whenever the function to set a new page number is called or if the header is accessed through the code, an extra paragraph is appears in the first page of the document which causes all the following pages to overflow. Hence, returning the false page number (in most cases total page + 1).

I wanted to know if this could be avoided. Or if i am doing anything wrong.
Attached Images
File Type: png 0.png (11.6 KB, 21 views)
File Type: png after.png (12.5 KB, 21 views)
Reply With Quote
  #2  
Old 11-18-2024, 04:15 AM
macropod's Avatar
macropod macropod is offline Setting page numbers Windows 10 Setting page numbers Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,366
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 something along the lines of:
Code:
Sub AdjustpageNumbers()
With ActiveDocument.Sections.First
  .PageSetup.DifferentFirstPageHeaderFooter = True
  .PageSetup.OddAndEvenPagesHeaderFooter = True
  With .Headers(wdHeaderFooterFirstPage).PageNumbers
    .StartingNumber = 84
    .RestartNumberingAtSection = True
  End With
  With .Headers(wdHeaderFooterEvenPages)
    .PageNumbers.NumberStyle = wdPageNumberStyleArabic
    .Range.Fields.Add .Range.Characters.Last, wdFieldPage, , False
  End With
  With .Headers(wdHeaderFooterPrimary)
    .PageNumbers.NumberStyle = wdPageNumberStyleArabic
    .Range.Fields.Add .Range.Characters.Last, wdFieldPage, , False
  End With
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 11-18-2024, 05:07 AM
Bikram Bikram is offline Setting page numbers Windows 10 Setting page numbers Office 2007
Advanced Beginner
Setting page numbers
 
Join Date: Jul 2021
Location: Nepal
Posts: 94
Bikram is on a distinguished road
Default

Thank you for the response, Macropod. I am very grateful. The code you provided inserts the page number, but the problem still exists, and it seems like (Hard-coding it to take as little space as possible) is the only option for now.

For now, I am using
Code:
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    With Selection
        .font.Size = 1
        .font.spacing = 0
        .ParagraphFormat.spaceBefore = 0
        .ParagraphFormat.spaceAfter = 0
        .ParagraphFormat.LineSpacingRule = wdLineSpaceMultiple
        .ParagraphFormat.lineSpacing = LinesToPoints(1)
    End With
Reply With Quote
  #4  
Old 11-18-2024, 01:14 PM
macropod's Avatar
macropod macropod is offline Setting page numbers Windows 10 Setting page numbers Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,366
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

I am unable to reproduce the problem. Perhaps you could upload a document with some representative data (delete anything sensitive) for testing? You do this via the paperclip symbol on the 'Go Advanced' tab at the bottom of this screen.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 11-20-2024, 06:11 AM
Italophile Italophile is offline Setting page numbers Windows 11 Setting page numbers Office 2021
Expert
 
Join Date: Mar 2022
Posts: 538
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

Quote:
Originally Posted by Bikram View Post
If we adjust the pagenumber manually the problem doesn't exist. But if we attempt it with code the first page header will be expanded and a paragraph will appear. Is there a way to change the page numbers without causing expansion of Header in the first page?

I have attached the snipped images of before and after running the code. Please have a look.
The issue is a result of your code. Until you ran your code that header did not exist. The empty header was created because your code uses the Selection object. This is one of the many reasons why use of the Selection should be avoided. Instead, you should access the objects directly, as demonstrated in the answer from macropod.
Reply With Quote
Reply

Tags
setting page numbers



Similar Threads
Thread Thread Starter Forum Replies Last Post
Even page numbers disappear when setting up opposite headers. slromine Word 2 08-02-2019 08:05 AM
Setting page numbers 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
Setting page numbers Page Numbers Not Matching Chapter Numbers gracie5290 Word 1 02-02-2012 11:41 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:18 PM.


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