Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / September 2006

Tip: Looking for answers? Try searching our database.

reset page number with VBA

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Derek - 25 Sep 2006 20:20 GMT
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
Jezebel - 25 Sep 2006 22:00 GMT
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.

> Word 2000
>
[quoted text clipped - 39 lines]
> Best wishes from Western Canada,
> /Derek
sugarboyrosnerd - 26 Sep 2006 00:58 GMT
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

> Check the properties of the PageNumbers object, which is itself a property
> of the HeaderFooter objects. You can set the StartingNumber value, and also,
[quoted text clipped - 50 lines]
> > Best wishes from Western Canada,
> > /Derek
Jezebel - 26 Sep 2006 02:33 GMT
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 }

> Hi Jezebel. Thank you, once again for your prompt valuable input. I've
> been
[quoted text clipped - 120 lines]
>> > Best wishes from Western Canada,
>> > /Derek
sugarboyrosnerd - 26 Sep 2006 15:47 GMT
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

> 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
[quoted text clipped - 130 lines]
> >> > Best wishes from Western Canada,
> >> > /Derek
Jezebel - 26 Sep 2006 22:43 GMT
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.

> Thanks Jezebel.
>
[quoted text clipped - 164 lines]
>> >> > Best wishes from Western Canada,
>> >> > /Derek
sugarboyrosnerd - 27 Sep 2006 15:52 GMT
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

> 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
[quoted text clipped - 178 lines]
> >> >> > Best wishes from Western Canada,
> >> >> > /Derek
Jezebel - 27 Sep 2006 22:13 GMT
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.

> Hi Jezebel.
>
[quoted text clipped - 231 lines]
>> >> >> > Best wishes from Western Canada,
>> >> >> > /Derek
sugarboyrosnerd - 28 Sep 2006 16:09 GMT
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

> 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).
[quoted text clipped - 245 lines]
> >> >> >> > Best wishes from Western Canada,
> >> >> >> > /Derek
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.