reset page number with VBA

D

Derek

Word 2000

Hello.

I'm creating a suite of automated templates for my company.

Users will not be able to access the header or footer because I've
implemented code to prevent direct user access to both. Find the code I use
here (if you are interested):

http://www.word.mvps.org/FAQs/Customization/ProtectWord2000PlusHeader.htm
(link graciously provided by Doug Robbins)

I need to be able to do 2 things.

1. Programmatically change the starting page number at the beginning of the
document and then have the page numbering change cascade through the
remaining page numbers. For example, if a user chose to start a document on
Page 1-101, my "Number Userform" would give the user the option of typing
"101", the user would click "OK" and all pages that follow 101 would number
in order.

2. I would like to also give the option to apply sectional OR sequential
page numbering.
Sectional Page Numbering Example: 1-1, 1-2, 1-3, 1-#.
Sequential Page Numbering Example: 1, 2, 3, #.

Some details: My page numbers use the PAGE field code and are located within
textboxes in the footer.

I DO have a macro that is capable of updating all fields within textboxes in
all footers.

Any assistance you can provide is very much appreciated.

Best wishes from Western Canada,
/Derek
 
J

Jezebel

Check the properties of the PageNumbers object, which is itself a property
of the HeaderFooter objects. You can set the StartingNumber value, and also,
optionally, set the numbering to include the section number and restart for
each section.

Bear in mind that your code to keep users out of the headers and footers is
very simply by-passed: all the users need to do is set macro security to
high, so the code doesn't run. A simpler method is to put a continuous
section break at the very start of the document, then protect section 1 for
forms.
 
S

sugarboyrosnerd

Hi Jezebel. Thank you, once again for your prompt valuable input. I've been
reading your posts for almost a year and finally decided to join.

Based on your feedback, this is what I came up with.

With ActiveDocument.Sections(1) _
.Footers(wdHeaderFooterPrimary).PageNumbers
.NumberStyle = wdPageNumberStyleArabic
.IncludeChapterNumber = True
'Kill page prefix if Sequential is picked
'D's Note: optSequential is a option button on my (Numbering Wizard)
userform
If optSequential.Value = True Then
.IncludeChapterNumber = False
End If
.RestartNumberingAtSection = True
'D's Note: txtPagestart is a textbox on my (Numbering Wizard) userform
.StartingNumber = txtPagestart
End With

This completely answered my question.

It would be even better if I could add code that will automatically restart
the page numbering every time a new page number prefix (that is, a new
outlined Heading 1 number) appears in document prose.

Currently, the code finds the resets the page number for the first section
of the active document only.

Assuming the user wants to use Sectional (as opposed to Sequential) page
numbering, the code needs to:

-determine if there is a new page number prefix
(this is true if the Heading1 outline level number is different from the
previous page)
- ... and then reset the page numbering to 1 (that is, the page number
suffix) if the above condition is true

For example,
Instead of the active document containing the following pages numbers:
1-1, 1-2, 2-3, 2-4, 2-5, 3-6, #-7, etc
I would like to see:
1-1, 1-2, 1-3, 1-4, 2-1, 2-2, 3-1

Is this possible?

I apologise for such a lengthy post ... and I must thank you "in person" for
your previous "update a field within a textbox within a header" post.

BTW, I haven't tried the continuous section break trick you mentioned, but I
will and will let you know the results.

Best,
/Derek
Best wishes from Western Canada
 
J

Jezebel

There's no method I know of to set the document up in advance so that each
section is numbered separately. You can do it after the event, with the
PageNumbers settings you've got, by inserting a Section Break before each
chapter heading.

Another method is to bookmark each chapter heading (Word does this
automatically if you have a TOC), then use some field arithmetic:

{ = { PAGE } - { PAGEREF "_toc123456789" } + 1 }
 
S

sugarboyrosnerd

Thanks Jezebel.

I'll add an odd page break to the Heading 1 style. Then I'll try to figure
out how to iterate through each section that starts with Heading 1 style and
reset the page number suffix (assuming numbering is sectional) to 1.
Any hints on how I can incorporate my existing code to do this?

Also, I tried the continuous section break trick you suggested. I put a
continous section break at the beginning of my document and then protected
the document for forms. I must be doing something wrong because the entire
document is protected and I am not able to type anything in the headerfooter
or story.
Any advice you can offer is appreciated.

Thank you,
/Derek
Best wishes from Western Canada
 
J

Jezebel

If your document is divided into sections (with section breaks), you don't
need to reset the page numbers. That happens automatically for all sections
if you set .RestartNumberingAtSection = True.

You can iterate the sections using for ...each on the document's Sections
collection. You can find all the heading 1s by using the Find object, or
simply iterating all the paragraphs and checking the Style name.

The continuous section break trick: make sure you protect *only* section 1
for forms. If you can't type in the rest of the document, then you've
protected section 2 also. The point of this method is that it doesn't use
code. If you're running code anyway, then Graham's method is probably
better.
 
S

sugarboyrosnerd

Hi Jezebel.

Here's what I came up with. I get an error when I run this code.
I'm not sure what I'm doing wrong.
You mentioned that I need to find all the Heading 1s, but I'm not sure how
to do this.
Any help you can offer is appreciated.

For Each Section In ActiveDocument.Sections
.Footers(wdHeaderFooterPrimary).PageNumbers
.NumberStyle = wdPageNumberStyleArabic
.IncludeChapterNumber = True
'Kill page prefix if Sequential is picked
If optSequential.Value = True Then
.IncludeChapterNumber = False
End If
.RestartNumberingAtSection = True
.StartingNumber = txtPagestart
'End With
Next


.... regarding the continous section break trick, I think I'll go with my
protect headerfooter code instead. You are correct when you say that users
can circumnavigate the protection by raising macro security settings, but
they will also disable essential document-building automation.

Thank you and best wishes from Western Canada,
/Derek
 
J

Jezebel

The problem with your code is that you're not specifying the object to which
all the dot-references belong. (ie the With ... End With bit).

Separately, I think you haven't understood what you're trying to do. Do some
experiments with document sections and page numbers: if the document
contains section breaks, it's sufficient to set the numbering properties for
the first section -- these settings will be inherited by all following
sections unless you explicitly change them for a subsequent section.

The reason I suggested searching for Heading 1s, was to insert section
breaks before them. If your document already has section breaks, there's no
need to to do. To find Heading 1s, either use Find (again, experiment!) or
iterate the paragraphs and check the style of each.
 
S

sugarboyrosnerd

OK Jezebel. The skill of fishing is much more valuable than a single fish.
I'll keep experimenting and let you know when I figure out how to make this
work.
Best,
/Derek
 

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