Need VBA Macro to Loop through pages and print counter in footer of each page

Joined
Oct 19, 2017
Messages
4
Reaction score
0
Need VBA Macro to Loop through pages and print counter in footer of each page.

The basic need is to insert a visible sequential number in the footer of each page that equates to the page's position in the entire document in a multi-section document with section based page numbering.

I have tried to achieve this through fieldcodes to no avail. A fieldcode solution would be preferable.

However, a macro that could be run before saving the document as a final document would work too.

I have a programming background. However, I have not done any significant coding since Microsoft introduced Document models. I am familiar with object modeling but not the specific implementation in Word. As a very simple macro approach, if i knew the proper object model variables and functions to use, I would:

Start.
Create variable "counter".
Set counter to 1.
Go to first page.
Print counter in footer.

Loop
Increment counter.
Go to to next page.
Print counter in footer.
If last page then endloop else loop.
Stop.

I would also need to know how to specifically position the inserted counter into the footer. It will have prefix that will be document specific. With the fieldcode solution I was trying: <prefix>.<fieldcode>. This works fine in the core document (leaves an increment problem) but not in the footer. I was hoping the footer would handle the incrementing but it doesn't seem to be implemented in a manner to support this.

Simplicity is better than elegance in this case, since I might not be able to fully follow the elegant solution and I may want to tweak minor details. Both would be OK.

Thanks from the old guy.
 

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
578
Reaction score
50
Simply insert your 'prefix' and a PAGE numbering field in the footer...
You cannot do what you want the way you envisage, since updating the footer on one page will update it with the same content on all other pages using the same footer - including in other Sections whose footers are linked with the current one.
 
Joined
Oct 19, 2017
Messages
4
Reaction score
0
I can manually create a text box, set position relative to page and drag it over the footer area. It then appears over the top of the footer area. Can I programmatically create a box, position it, and insert the page numbering into the text box inside the loop? It seems reasonable to believe that the object model would allow this.

This issue can be separated into to separate issues 1) how to loop and identify page numbers and 2) how to place the page number in an appropriate position on each page.
 

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
578
Reaction score
50
Why are you bothering with any of that when all you need is a single PAGE field in the page header?
 
Joined
Oct 19, 2017
Messages
4
Reaction score
0
Because the page field varies by section. My documents have three sections by default, each with its own numbering that restarts at one. I need a unique reference number on each page for research purposes. The reference number needs to the the exact same format on each page. It can't be, go to page "i" or page "1" or page "I".

I cannot believe that Microsoft does not have a hidden field that uniquely numbers "identifies" each page. Otherwise, how do you loop through the pages. I don't understand why they would leave that out of the available field codes. Regardless, this is the need. It is a business need that I need a solution for.
 

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
578
Reaction score
50
Regardless of how many Sections your documents have, you can use continuous page numbering. You would only not be getting that if you've changed the defaults...
 

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
578
Reaction score
50
The topic concerns Word, not One Note. If you have a One Note question, you should post it in the One Note forum...
 
Joined
Oct 19, 2017
Messages
4
Reaction score
0
This is a word issue. I need the sections numbered differently for specific reasons, and I need a continuous document page number for reference purposes. This is not either or. I need both.
 

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
578
Reaction score
50
Even if you were to do what you want with the textboxes, you couldn't reference them unless you also bookmark the numbers - with a different bookmark for each one. Worse, if you open the document on a computer using a different printer driver (e.g. because it uses a different printer or OS) or if you switch printers on the same computer, the pagination is liable to change and you could easily end up with your textboxes on the wrong pages - some may have none whilst others have two! That's because Word uses data from the active printer driver to optimise the layout. If that's not bad enough, it'd be even worse for a document created in Word 2013 & later then opened in Word 2010 or earlier, as MS changed Word's layout engine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top