Consulting

Results 1 to 5 of 5

Thread: page numbering

  1. #1
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location

    page numbering

    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

  2. #2
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    It's telling me that a member of the collection does not exist????

  3. #3
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •