PDA

View Full Version : [SOLVED:] page numbering



Kilroy
10-24-2016, 09:01 AM
I have the following code that worked for a while but now for some reason it doesn't? Anyone have any ideas?


Sub PageNumbers()
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
10-28-2016, 07:12 AM
It's telling me that a member of the collection does not exist????

gmayor
10-28-2016, 09:29 PM
Check the building blocks organizer to ensure that the building block still exists.
Or you could simply insert the fields in the footers e.g.


Sub PageNumbers()
Dim oFooter As HeaderFooter
Dim oRng As Range
For Each oFooter In ActiveDocument.Sections(1).Footers
If oFooter.Exists Then
Set oRng = oFooter.Range
With oRng
.ParagraphFormat.Alignment = wdAlignParagraphRight
.Text = "Page "
.Collapse 0
.Fields.Add oRng, wdFieldPage, , False
.End = oFooter.Range.End
.Collapse 0
.Text = " of "
.Collapse 0
.Fields.Add oRng, wdFieldNumPages, , False
End With
End If
Next oFooter
lbl_Exit:
Set oFooter = Nothing
Set oRng = Nothing
Exit Sub
End Sub

gmaxey
10-30-2016, 08:49 AM
Kilroy,

What is telling you that? When you have a problem and post looking for and answer, it helps if you step through your code using F8 and identify the specific line that throws the error.

In this case it is practically obvious. The likely cause in this case for the error is that you have not done something previous that loads your buildingblock templates. You can handler this error as follows:


Sub PageNumbers()
Dim oRng As Range
Set oRng = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
On Error GoTo Err_Handler
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
lbl_Exit:
Exit Sub
Err_Handler:
Templates.LoadBuildingBlocks
Resume
End Sub


or avoid it by ensuring that the building block templates are loaded when Word starts:


Sub AutoExec()
Templates.LoadBuildingBlocks
lbl_Exit:
Exit Sub
End Sub

Kilroy
11-26-2016, 02:03 PM
Thanks guys. Greg I wasn't aware of the trick with the F8 button thanks for pointing that out. I won't post without stating the problem line now.