#1
|
|||
|
|||
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 Thanks |
#2
|
||||
|
||||
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 |
#3
|
|||
|
|||
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 Thanks |
#4
|
||||
|
||||
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 |
#5
|
|||
|
|||
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 So I changed it to left to appear on the right (vice versa) Code:
oRng.ParagraphFormat.Alignment = wdAlignParagraphLeft 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 Thanks |
#6
|
||||
|
||||
Your request stated
Quote:
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 |
#7
|
|||
|
|||
Of course , I am using right to left language as a secondary language, but I set my normal template file, left to right direction.
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 |
#8
|
||||
|
||||
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 |
#9
|
|||
|
|||
Quote:
It works perfectly Best Regards |
#10
|
|||
|
|||
Are you aware that there are already components in Word that could do what you want without vba or to be used with vba?
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Updating page number in table cells in footer in each page | nyconfidential | Word VBA | 1 | 08-13-2022 03:37 PM |
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 |
Footer for page numbers recently started showing a colored field when adding page numbers | thefonebug | Word | 12 | 10-24-2016 05:18 AM |
Total Page number incorrect in the first page footer | mmathisekar | Word | 11 | 06-16-2016 06:00 AM |