Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-13-2023, 01:48 PM
laith93 laith93 is offline Adding footer page number with custom style Windows 10 Adding footer page number with custom style Office 2019
Competent Performer
Adding footer page number with custom style
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Default Adding footer page number with custom style

Hi,
I want a macro to add a footer with the page number on the right, the distance from the bottom (1cm), the style of footer text (Times new roman, 14pt), I tried and used this macro, but didn't fulfill my requirement

Code:
Sub AddFooterWithPageNumber()
    Dim section As section
    Dim footer As range
    
    ' Loop through each section in the document
    For Each section In ActiveDocument.Sections
        Set footer = section.Footers(wdHeaderFooterPrimary).range
        
        ' Clear existing content in the footer
        footer.Text = ""
        
        ' Set font properties
        With footer.Font
            .Name = "Times New Roman"
            .Size = 14
        End With


        Dim positionFromBottom As Double
        positionFromBottom = CentimetersToPoints(1)
        
        footer.Fields.Add range:=footer, Type:=wdFieldPage
        
        ' footer.Text = " - "  ' Uncomment this line if you want to add a separator
       
        
'         Update the footer
'        footer.Updates
    Next section
End Sub
Any help, suggestions
Thanks
Reply With Quote
  #2  
Old 12-13-2023, 03:13 PM
Guessed's Avatar
Guessed Guessed is offline Adding footer page number with custom style Windows 10 Adding footer page number with custom style Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

When you say it doesn't fulfill your requirement, can you explain what it wasn't doing?

I can see you only touch the primary footer without seeing if the other two footers are turned on. Nor are you looking at link to previous settings. positionfrombottom is created as a variable but not used. The actual location of the text in the footer will depend on the setting for footer distance + paragraph space after + a bit of the line spacing mixed with the typeface and font size.

I normally do this sort of thing by first storing my preferred footer as a building block and then using a macro to insert that building block wherever I want it.

So assuming you have created a footer exactly how you want it, and then saved that as a building block in your Normal template with the name myFoot, you could then use this code
Code:
Sub AddFooterWithPageNumber()
  Dim aSect As section, aFoot As HeaderFooter
  For Each aSect In ActiveDocument.Sections       ' Loop through each section in the document
    aSect.PageSetup.FooterDistance = CentimetersToPoints(1)
    Set aFoot = aSect.Footers(wdHeaderFooterPrimary)
    If Not aFoot.LinkToPrevious Then
      NormalTemplate.BuildingBlockEntries("myFoot").Insert Where:=aFoot.Range, RichText:=True
      aFoot.Range.Paragraphs.Last.Range.Delete
    End If
  Next aSect
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 12-13-2023, 09:01 PM
laith93 laith93 is offline Adding footer page number with custom style Windows 10 Adding footer page number with custom style Office 2019
Competent Performer
Adding footer page number with custom style
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Default

Thank Mr. Adrew
My requirement is to add a footer with the page number on the right, the distance from the bottom (1cm), the style of footer text (Times new roman, 14pt)
Dear Mr. Andrew,
After I created the "myfoot" building block, and running your code
an error appears " the requested member of collection doesn't exists"
after I changed myfoot category, as follows
81.png
The error was gone, but nothing happened
Dear Andrew, can you use this attributes,
Code:
        ' Set font properties
        With footer.Font
            .Name = "Times New Roman"
            .Size = 14
        End With
I think it's better and easier
Thanks
Reply With Quote
  #4  
Old 12-14-2023, 01:32 AM
gmayor's Avatar
gmayor gmayor is offline Adding footer page number with custom style Windows 10 Adding footer page number with custom style Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

How about
Code:
Sub AddFooterWithPageNumber()
    Dim oSection As section
    Dim oFooter As HeaderFooter
    Dim oRng As Range
    
    ' Loop through each section in the document
    For Each oSection In ActiveDocument.Sections
        oSection.PageSetup.FooterDistance = CentimetersToPoints(1)
        For Each oFooter In oSection.Footers
            If oFooter.Exists Then
                Set oRng = oFooter.Range
                oRng.Fields.Add oRng, wdFieldPage, , False
                oRng.ParagraphFormat.Alignment = wdAlignParagraphRight
                ' Set font properties
                With oRng.Font
                    .Name = "Times New Roman"
                    .Size = 14
                End With
            End If
        Next oFooter
    Next oSection
lbl_Exit:
    Set oSection = Nothing
    Set oFooter = Nothing
    Set oRng = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 12-18-2023, 07:16 AM
laith93 laith93 is offline Adding footer page number with custom style Windows 10 Adding footer page number with custom style Office 2019
Competent Performer
Adding footer page number with custom style
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
How about
Thanks so much, Mr. Graham,
It works perfectly, but just for clarification
the page number is placed on the left, although the
Code:
oRng.ParagraphFormat.Alignment = wdAlignParagraphRight
84.png
So I changed it to left to appear on the right (vice versa)
Code:
oRng.ParagraphFormat.Alignment = wdAlignParagraphLeft
So I don't know if it's associated with file settings or Windows settings.

Lastly,
Dear Graham, I tried to add custom text using
Code:
    Dim aRng As range
                Set aRng = oFooter.range
                aRng.Fields.Add aRng, wdFieldIncludeText, "laith", True
but I get many error, not specified, not valid ...etc
Thanks
Reply With Quote
  #6  
Old 12-18-2023, 10:04 AM
gmayor's Avatar
gmayor gmayor is offline Adding footer page number with custom style Windows 10 Adding footer page number with custom style Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Your request stated
Quote:
My requirement is to add a footer with the page number on the right
However that is easy to change as you have suggested.
If the setting is reversed, my guess is that you are using a right to left language.

How does the text "laith" relate to the page number?
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #7  
Old 12-18-2023, 10:17 AM
laith93 laith93 is offline Adding footer page number with custom style Windows 10 Adding footer page number with custom style Office 2019
Competent Performer
Adding footer page number with custom style
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
my guess is that you are using a right to left language.
Of course , I am using right to left language as a secondary language, but I set my normal template file, left to right direction.

Quote:
Originally Posted by gmayor View Post
How does the text "laith" relate to the page number?
Excuse me Mr. Graham, I just wanted to add additional custom text to the page number on the opposite side (page number on left, custom text on right).
Thanks
Reply With Quote
  #8  
Old 12-18-2023, 11:52 PM
gmayor's Avatar
gmayor gmayor is offline Adding footer page number with custom style Windows 10 Adding footer page number with custom style Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

OK. The default footer has tabs at the centre and right so the simplest approach is to use that and add two tabs and the text to the end of the range e.g.


Code:
Sub AddFooterWithPageNumber()
    Dim oSection As section
    Dim oFooter As HeaderFooter
    Dim oRng As Range
    
    ' Loop through each section in the document
    For Each oSection In ActiveDocument.Sections
        oSection.PageSetup.FooterDistance = CentimetersToPoints(1)
        For Each oFooter In oSection.Footers
            If oFooter.Exists Then
                Set oRng = oFooter.Range
                ' Set font properties
                With oRng.Font
                    .Name = "Times New Roman"
                    .Size = 14
                End With
                oRng.Fields.Add oRng, wdFieldPage, , False
                oRng.Collapse wdCollapseEnd
                oRng.Text = Chr(9) & Chr(9) & "laith"
            End If
        Next oFooter
    Next oSection
lbl_Exit:
    Set oSection = Nothing
    Set oFooter = Nothing
    Set oRng = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #9  
Old 12-21-2023, 11:22 PM
laith93 laith93 is offline Adding footer page number with custom style Windows 10 Adding footer page number with custom style Office 2019
Competent Performer
Adding footer page number with custom style
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
OK. The default footer has tabs at the centre and right so the simplest approach is to use that and add two tabs and the text to the end of the range e.g.
Thanks so much dear Graham
It works perfectly
Best Regards
Reply With Quote
  #10  
Old 12-22-2023, 09:35 AM
Charles Kenyon Charles Kenyon is offline Adding footer page number with custom style Windows 11 Adding footer page number with custom style Office 2021
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,140
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

Are you aware that there are already components in Word that could do what you want without vba or to be used with vba?
  • Page number style - this is a character style built into Word which can be modified
  • You could create and implement a Footer building block that has your components. That Building Block can be a part of your document template and used manually Insert > Footer or using vba.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding footer page number with custom style Updating page number in table cells in footer in each page nyconfidential Word VBA 1 08-13-2022 03:37 PM
Adding footer page number with custom style Page number shows up as { PAGE } instead of number and question about area allowed for footer dw85745 Word 3 01-16-2022 03:19 PM
Don't want page number or date in footer on first page, do want graphic in header. dianahbr Word 2 02-23-2018 09:25 AM
Adding footer page number with custom style Footer for page numbers recently started showing a colored field when adding page numbers thefonebug Word 12 10-24-2016 05:18 AM
Adding footer page number with custom style Total Page number incorrect in the first page footer mmathisekar Word 11 06-16-2016 06:00 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:16 AM.


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