Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

inserting pagenumber as text

Status
Not open for further replies.

weeze2

Technical User
Jun 18, 2002
129
DK
Hi!

How does one with VBA insert pagenumber/pages as TEXT.

The reason i am trying to do this is because when unlinking all fields and shapes in the document, the pagenumber in the pagefoot is unlinked and all the pages gets the same "page"/"pages" value.

Can anyone help...or have another idea?

Thanks!
 
Hi weeze2,

One could, of course, run through a document and hard code a page number (and a total number of pages) on each page - it couldn't (easily) be in a page footer, though - but that would cease to be guaranteed correct once changes were made to the document (or a different printer used). It really isn't the best way to set up a document.

You don't say why you are unlinking all fields including page numbers. Is it really necessary to unlink them? Even if it is, why can you not re-insert a page-number field (rather than text) afterwards?

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
I could try...but another thing:

is the pagenumber in the documents footer a single object that reappears throughout the rest of the documents footer objects? The reason I ask is because text seems to be 'copied' to all the footters, and when the pagenumber loses its dynamics (by unlinking), the same page number is in all the pages footers.
 
Hi weeze,

The short answer to the question is No, not really, but it isn't really the right question.

What I believe happens when you unlink is that an immediate snapshot is taken and the value of the page number is that of whichever page you happen to be on at the time. Every occurreence of a {PAGE} field is then replaced by that value, regardless of the PageNumber object or the number of Footers in the Document or anything else.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
1. Are you actually using the {PAGE} field?

2. As Tony mentions, the page number in footers is an odd duck - mostly because of the way the Word object model handles header/footer objects. If you clarify FULLY, exactly what you are doing with the footers, perhaps we can assist better.

My usual rant: here is an example of the odd (until you really grasp the object model) behaviour of Word. IMPORTANT: The Section number value is actually calculated using VBA.

SECTION 1 - set for Different first page, Different Odd/Even. The following is the actaul text in the footers.

First Page: "This is First Page Section 1.
Odd Pages: "This Odd Page Section 1"
Even Pages: "This is Even Page Section 1"

Section 2 - set for Different first page (also set as NOT Same as previous), NOT set for Odd/Even. Main footer also set as NOT Same as previous.

First Page: "This is First Page Section 2"
All Other Pages: "This is the other pages Section 2"

If you now CHANGE Section 2 to have Different Odd/Even, the DEFAULT will look like this.

First Page: "This is First Page Section 2"
Odd Pages: "This is the other pages Section 2"
Even Pages: "This is Even Page Section 1"

Weird isn't it? However, it does in fact make sense with the way the object model is designed. Further to this, the PageNumber is a child object of the Header/Footer object, which in turn is a child of the Section object. So one thing to check is whetehr there are any Section breaks, and if so, is the page number set to continue from previous section. Unlinking this will revalue the page number.

Gerry
See my Paintings and Sculpture
 
Hi folks,

If you have a page number field in the header or footer of a document, about the only way you can preserve the page numbering when fields are unlinked in to turn each page into a stand-alone section before the page numbers are unlinked.

Perhaps the real issue is whether there is any need to unlink the page numbers, as doing so as per the above leaves one with a most inflexible document. Perhaps the fields in the footer don't need to be unlinked. For example, the following code will unlink fields in the body of the document, headers and shapes, but not in footers:

Sub LockFields()
Dim oSection As Section
Dim shp As Shape
Dim oHead As HeaderFooter
ActiveDocument.Fields.Unlink
For Each oSection In ActiveDocument.Sections
For Each oHead In oSection.Headers
If Not oHead.LinkToPrevious Then oHead.Range.Fields.Unlink
Next
Next
If ActiveDocument.Shapes.Count > 0 Then
For Each shp In doc.Shapes
With shp.TextFrame
If .HasText Then
.TextRange.Fields.Unlink
End If
End With
Next
End If
End Sub

Cheers
 
Thanks everyone!

Well im using {PAGE}/{NUMPAGES}
Ill try the last mentioned by trying to preserve just the pagenumbers. Some documents already contains many sections, so adding more sections migt be a bit hard to manage.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top