PDA

View Full Version : [SOLVED:] Inserting page numbers



Kilroy
09-19-2016, 04:31 AM
I've been trying use the macro recorder to insert page numbers (1 of x) in the footer. The recorder is giving me weird code. It works but I'm concerned with the line ""C:\Users\kilroy\AppData\Roaming\Microsoft\Document Building Blocks\1033\16\Built-In Building Blocks.dotx"" I'm not sure that if I'm not using this macro on my computer it will work. Is there any way to simplify??


Sub PageNumbers()
'
' PageNumbers Macro
' PageNumbers
'
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Application.Templates( _
"C:\Users\******xx\AppData\Roaming\Microsoft\Document Building Blocks\1033\16\Built-In Building Blocks.dotx" _
).BuildingBlockEntries("Bold Numbers 3").Insert Where:=Selection.Range, _
RichText:=True
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.EscapeKey
End Sub

gmayor
09-19-2016, 06:32 AM
If you change the line to
Application.Templates( _
Environ("APPDATA") & "\Microsoft\Document Building Blocks\1033\" & Val(Application.Version) & "\Built-In Building Blocks.dotx" _
).BuildingBlockEntries("Bold Numbers 3").Insert _
Where:=Selection.Range, _
RichText:=True
It will be transportable. You could however create the same effect with a simpler code
Sub PageNumbers2()
Dim oRng As Range
Set oRng = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
Application.Templates(Environ("APPDATA") & "\Microsoft\Document Building Blocks\1033\" & _
Val(Application.Version) & _
"\Built-In Building Blocks.dotx").BuildingBlockEntries("Bold Numbers 3").Insert _
Where:=oRng, RichText:=True
End Sub

Kilroy
09-19-2016, 06:50 AM
Thanks Graham the second code works great. I'm grateful for your help.