Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-17-2025, 10:40 PM
gcp gcp is offline Word page numbering Windows 11 Word page numbering Office 2021
Novice
Word page numbering
 
Join Date: Mar 2025
Posts: 16
gcp is on a distinguished road
Default Word page numbering

I recorded a macro to number pages. it includes:
Application.Templates( _
"C:\Users\gcpri\AppData\Roaming\Microsoft\Docu ment Building Blocks\1033\16\Built-In Building Blocks.dotx" _
).BuildingBlockEntries("Brackets 1").Insert Where:=Selection.Range, _
RichText:=True



When I run the macro it highlights the above in yellow and says:
"Run-time error '5941'. The requested member of the collection does not exist."
The cursor stops in the document in the footnote where the page number is to be inserted.
I have recorded the macro several times and occasionally the VB Editor refers to the building blocks but it does not stay there when I record other macros.
Allied to this is, when I record a macro can I allocate a project (or create a project) to contain the macro. The online help suggests that I have to go to the editor, start a new module and then cut and paste the code into that module.
Reply With Quote
  #2  
Old 05-17-2025, 11:52 PM
gmayor's Avatar
gmayor gmayor is offline Word page numbering Windows 10 Word page numbering Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,137
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 ofgmayor has much to be proud of
Default

Frankly I wouldn't bother with inserting a building block by macro, or rely on the recorder, but to achieve the same result with a macro, try the following:
Code:
Sub InsertPageNumInHeader()
Dim oRng As Range
    Set oRng = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
    With oRng
        .ParagraphFormat.Alignment = wdAlignParagraphCenter
        .Text = "[]"
        .Collapse 1
        oRng.Start = oRng.Start + 1
        .Fields.Add oRng, wdFieldPage, "", False
    End With
    Set oRng = Nothing
End Sub
And yes, the online help is correct about the macro recorder. See Installing Macros
__________________
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
  #3  
Old 05-18-2025, 09:23 PM
gcp gcp is offline Word page numbering Windows 11 Word page numbering Office 2021
Novice
Word page numbering
 
Join Date: Mar 2025
Posts: 16
gcp is on a distinguished road
Default Word page numbering

Thanks for that. I want to insert the page number in a footer, so I changed .Headers to .Footers and it worked fine.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Numbering Page in Center of Page in Word AdmirableWizard Word 3 04-26-2022 10:55 AM
Word 2019 & Page Numbering PapaTom Word 18 12-13-2019 08:02 AM
Word page numbering Word 10 Page Numbering DarenKF Word 2 11-08-2017 01:43 AM
Word page numbering Page-numbering question: continue numbering from one section to the next? {PAGE} of {SECTIONPAGES} SDwriter Word 12 10-25-2017 06:56 AM
Word page numbering Page Numbering in Word dickmill Word 10 01-11-2016 04:10 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:23 AM.


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