PDA

View Full Version : Solved: Fields in headers & footers.



geedee65
11-07-2007, 05:49 AM
The attached template has code (obtained from searching this forum) that unlinks fields in headers & footers when a new document is opened from it.
What I would like to do is to be able to keep the links for some fields, whilst the remainder are unlinked?

The "print" fields in the header and the "Page x of y" in the table in the footer are the fields I want to remain as fields and NOT to be unlinked.

I've put together the code in this template from help and searches here, but I just can't figure this part out!

fumei
11-07-2007, 02:55 PM
For Each oHeader In oSection.Headers
If oHeader.Exists ThenThe second line is a misconstruction.

There are three headers in each Section. There are always three headers in each Section. You can not delete them. Ever.

The Exist property is NOT about if the header exists, but if it is toggled as being used. That is, is it checked in Page Setup.

Primary can not be toggled, therefore Exist for it is always True.

Say you check Different first page. Put something in it. "This is first page text."

If you checked for Exist, it would be True.MsgBox ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage) _
.Range.Text

would display:

"This is first page text."

OK? Now UNCHECK Different first page. There is nothing there now. Right? Not really.

If you checked for Exist, it would be False. However, if you ran:MsgBox ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage) _
.Range.Text

it would STILL display:

"This is first page text."

The content of the three different headers persists. To repeat, .Exist tests to see if that header is being used, NOT if it "exists".

Since your document has NO different headers being used, there is no point in the Exist line. You are only using Primary, and it always exists.

OK, as for your unlinking some fields, but not others, you can do this by checking the .Code of each field.

BTW: you mention "Page x of y", but you are doing ti with two fields, Pages, and NumPages.

BTW 2: you use the comment
" 'Look in each header section and unlink fields to leave just text"

Um, there is no such thing as a "header section".

For Each oHeader In oSection.Headers
For Each oField In oHeader.Range.Fields
If Instr(oField.Code, "PRINTDATE") = 0 Then
oField.Unlink
End If
Next oField
Next oHeaderBut again, I am not sure why you bother doing Next oHeader, as you are only using Primary.

So, really, you could use:For Each oField In ActiveDocument.Sections(1).Headers(1).Range.Fields
If Instr(oField.Code, "PRINTDATE") = 0 Then
oField.Unlink
End If
Next oFieldwhich would Unlink any field that does NOT have PRINTDATE in it.

For Each oField In ActiveDocument.Sections(1).Footers(1).Range.Fields
If Instr(oField.Code, "Pages") = 0 Or _
Instr(oField.Code, "NumPages") = 0 Then
oField.Unlink
End If
Next oFieldwhich would Unlink any field that does not contain "Pages", OR "NumPages".

geedee65
11-08-2007, 03:18 AM
Thanks for that Gerry, especially the explanation of how sections, headers & footers work, it certainly made it easier understanding the code.

The header worked a treat, but for some reason the footer didn't, after a little messing around I came up with this,
For Each oField In ActiveDocument.Sections(1).Footers(1).Range.Fields
If InStr(oField.Code, "PAGE") = 0 Then
oField.Unlink
End If
Next oField


I'm not sure why "PAGE" applied to both Page & NumPages fields, but it works!

TonyJollans
11-08-2007, 03:46 AM
Perhaps because it's called numPAGEs.

fumei
11-09-2007, 03:15 PM
Yeah...I was going to post a better line myself, as there is no need for TWO instructions, as PAGE is in both.

Doh!