PDA

View Full Version : Update Fields problem Revisited



Bilby
04-06-2006, 04:22 PM
Greetings,

Recently I had to get around to solving a problem with NumPages field which refused to automatically update if it was in the main body of the document (I'm using Word XP SP2).

I found that I had to test for a wdFieldNumPages field Type , then use the
"\line" bookmark to select the entire line (.Select doesn't work). Then use the selection object to update.

The code was then dumped into a custom Print Preview routine to force the update and it now works reliably and consistently.

My question is; Does anyone know why? The only post I found was back in 2004. I have a workable solution but I just don't understand WWWWWWHHHHHYYYYY!!!!!!!

fumei
04-06-2006, 10:42 PM
NumPages does not automatically update outside of Headers / Footers. You CAN update it easily with:Sub UpdateFields()
Dim oField As Word.Field
For Each oField In ActiveDocument.Fields()
oField.Update
Next
End Subwhich will update all fields in the main body - or main Story. You certainly do not need to check its Type, or do any selecting, or forcing a Print Preview...ugh, yuck!

The WHY is that PageNumbers are child objects of the HeaderFooter, not the main Story. Yes, you can put the field anywhere you like, but the value comes from the HeaderFooter.

It is important to remember that EVERY page has a header/footer - whether you like it or not.

Bilby
04-10-2006, 03:24 PM
Funnily enough I almost agree with you. In simpler docs where I just paste the offending field code into a clean document and add page breaks you are absolutely right. In fact simply going to print preview (Word standard one) is all thats needed.

But the offending document isn't that simple. The test I use works and even though the only working bit is the .Update I've had to go 'over the top' to ensure that it ALWAYS updates.

Anyway thanks for your input, the fact that its a child of H/F and not the main story sorta makes sense to me.

fumei
04-10-2006, 06:06 PM
The test I use works and even though the only working bit is the .Update I've had to go 'over the top' to ensure that it ALWAYS updates.Care to explain that?