PDA

View Full Version : SECTIONPAGES displaying incorrectly



ukemike
09-29-2005, 12:27 PM
Hey guys

I have a weird problem. I am generating (find/replace on certain keywords) a word document in c# (yes, the same one as I have been have problems for like 6 months) and basically in the header or the page i enter {PAGE} out of {SECTIONPAGES}. After i generate the document the last page says 4 out of 3. Which is incorrect because it should be 4 out of 4. Now if i go to the first page and put the cursor someone in the first page the SECTIONPAGES somehow fixes itself. This does not happen all the time, sometimes it does not do anything.
I have tried using Repaginate VBA method. I tried turning off and on the codes at the end of the generation. Still nothing.
Am i missing something here?
Also this does not happen all the time. Sometimes the document will generate and all the page numbers will be correct.


Thanks
Mike

lucas
09-29-2005, 02:13 PM
Not sure if this will work but try:
wdFieldPage in place of {PAGE}
and
wdFieldNumPages in place of {SECTIONPAGES}
you will probably have to bracket them....

ukemike
09-29-2005, 02:21 PM
Hello lucas,

It says Bookmark not found.


Mike

MOS MASTER
09-29-2005, 02:25 PM
Hi Mike, :hi:

Which version of Word are we talking about here? MS has known bugs in 2000 for instance relating this problem. (I think SP2 or SP3 fixed the pagenumber update problem)

You can of course always refresh all Fields (Pagenumbers are fields) after your code has run of course. (In the code) :whistle:

TonyJollans
09-30-2005, 03:09 AM
I'll just re-iterate what Joost has said. There are certainly some funnies with page numbers in all versions of Word (as Joost says Word 200 SP2 or SP3 did fix things but the errors were reintroduced in Word 2002). But do make sure you have updated all fields before looking atthe values displayed.

Mechanisms for updating fields vary. If you want to do it from code make sure you update them all. You need to explicitly reference headers and footers separately from the body of the document.

ukemike
09-30-2005, 06:22 AM
Hey guys,
Appreciate the response. I am using the .dll from Word2000 but I am generating the word document in Word2003. So basically using Word2000 functions in 2003. This is because my machine has 2003 but the clients where this program will be ran at has 2000 and it needs to work on both. So that is the version info.
Could you show me some VBA code which i could use to update all the fields.


Thank you
Mike

TonyJollans
09-30-2005, 08:56 AM
Hi Mike,

Don't know the C# stuff but ..

document_ref.Fields.Update

will update fields in the body of the document, and ..

document_ref.Sections(1).Headers(1).Range.Fields.Update

will update fields in the first header in the first section. Replace Headers with Footers for footer fields. You'll need to explicitly reference each header and footer in each section that you are using - or could use a loop to get them all, something like ..With (document_ref)
For i = 1 To .Sections.Count: With .Sections(i)
For j = 1 To .Headers.Count: With .Headers(j)
If .Exists Then .Range.Fields.Update
End With: Next
For j = 1 To .Footers.Count: With .Footers(j)
If .Exists Then .Range.Fields.Update
End With: Next
End With: Next
End With

MOS MASTER
09-30-2005, 04:15 PM
Could you show me some VBA code which i could use to update all the fields.


Hi Mike, :hi:

All fields is very dificult cause there are special fields like Table of Contents fields that need a extra bit of code.

But normal fields like you talk about can be update in each storyrange of the document. (Header footer, main story, footnote, etc....) by:
Sub UpdateAllMyNormalFields()
Dim oRange As Word.Range
For Each oRange In ActiveDocument.StoryRanges
Do
oRange.Fields.Update
Set oRange = oRange.NextStoryRange
Loop Until oRange Is Nothing
Next

Set oRange = Nothing
End Sub


HTH, :whistle: