Unlinking Fields in Header

M

mcp6453

I have a Word 2000 template that uses InsertField to insert AutoText
into a header at creation time. After the document is open, I need to be
able to unlink the field in the header such that the header never
changes.

The UnlinkAllFields macro is ActiveDocument.Fields.Unlink. If I run it
from the open document, it ignores the header. If I open the header and
run it, nothing happens. If I select all text in the open header and run
it, the fields are unlinked.

Similar to this operation, if I open the header and run
Selection.Fields.Unlink, I don't have to select the field in the header
to unlink it.

Here's the question: Is there a way to unlink the fields in the header
without having to open the header, select the text, unlink the field,
and close the header?
 
P

Peter Hewett

Hi

Try this, there may be more code than you were expecting, but that's
because there are three different types of Headers (and footers) and they
can link to yet other headers. It all depends on the document structure.
Also, it's not a good idea to write code that varies depending on document
structure as changing the document can break the code!

Sub UnlinkFieldsInHeaders()
Dim Story As Variant
Dim rngNext As Word.Range

' Iterate through all story types
For Each Story In ActiveDocument.StoryRanges

' Only unlink fields in a header
If Story.StoryType = wdPrimaryHeaderStory Or _
Story.StoryType = wdFirstPageHeaderStory Or _
Story.StoryType = wdEvenPagesHeaderStory Then

' Unlink fields in this header
Story.Fields.Unlink

' There may be linked headers so unlink them as well
Set rngNext = Story.NextStoryRange
Do Until rngNext Is Nothing

' Unlink fields in this header
rngNext.Fields.Unlink

' Link to next story (if any)
Set rngNext = rngNext.NextStoryRange
Loop
End If
Next
End Sub


HTH + Cheers - Peter
 
M

mcp6453

Wow! No wonder I couldn't figure it out. Now for bonus points, how do I
need to add for footers? I apologize for not asking initially, but I
thought the code might be simple enough that I could modify it on my
own. However, you're using objects, properties, or methods (I cannot
distinguish them yet) that I have never heard of. Analyzing your code
below will be a great learning exercise, too. Thanks for taking the
time.
 
P

Peter Hewett

Hi

Here's a version for doing the same job for footers. As you can see the
ONLY difference is the StoryType constants. I'll leave it to you to merge
the two procedures if required:

Sub UnlinkFieldsInFooters()
Dim Story As Variant
Dim rngNext As Word.Range

' Iterate through all story types
For Each Story In ActiveDocument.StoryRanges

' Only update fields in a footer
If Story.StoryType = wdPrimaryFooterStory Or _
Story.StoryType = wdFirstPageFooterStory Or _
Story.StoryType = wdEvenPagesFooterStory Then

' Update fields in this footer
Story.Fields.Unlink

' There may be linked footers so unlink them as well
Set rngNext = Story.NextStoryRange
Do Until rngNext Is Nothing

' Update fields in this footer
rngNext.Fields.Unlink

' Link to next story (if any)
Set rngNext = rngNext.NextStoryRange
Loop
End If
Next
End Sub

Within the VBA IDE press F2 to display the Object Browser and in the
"Classes" pane browse for and select "WdStoryType". Or bring up the VBA
online help (assuming you installed it!!!!) and select "StoryType" on the
Index tab. This should get you going.

HTH + Cheers - Peter
 
M

mcp6453

Peter said:
Hi

Here's a version for doing the same job for footers. As you can see the
ONLY difference is the StoryType constants. I'll leave it to you to merge
the two procedures if required:

Thanks, Peter. No problem. I'm still mystified, however, in how you guys
know all of these objects, methods, and properties. Online help has the
information, if you know what you're looking for, which I don't!
 
P

Peter Hewett

Hi

As in all things nowt beats experience! I've been running my own Word/Office
consultancy business for over 7 years so I've got a headstart!

But as I said in my previous post explore the Word object model and use the
online VBA help - Install it if you have to as it's an absolute must. Don't
be afraid to try stuff and use the Immediate window also breakpoint your code
and explore it using the IDEs Locals window.

Word (as with all MSoft Office products) is VERY object oriented. So make
sure you get to grips the concepts and uses of Classes. It may seem steep at
first but persevere and grab a couple of good books on Word/VBA programming.

Good luck - Peter
 
M

mcp6453

Peter said:
Hi

As in all things nowt beats experience! I've been running my own Word/Office
consultancy business for over 7 years so I've got a headstart!

But as I said in my previous post explore the Word object model and use the
online VBA help - Install it if you have to as it's an absolute must. Don't
be afraid to try stuff and use the Immediate window also breakpoint your code
and explore it using the IDEs Locals window.

Word (as with all MSoft Office products) is VERY object oriented. So make
sure you get to grips the concepts and uses of Classes. It may seem steep at
first but persevere and grab a couple of good books on Word/VBA programming.

Good luck - Peter

Gotta ask. What's "nowt"?
 
C

Charles Kenyon

You might want to consider inserting AutoText rather than inserting an
AutoText field to start with. Then you would not have to unlink (unless the
AT entries, themselves, contain fields).
--

Charles Kenyon

See also the MVP FAQ: <URL: http://www.mvps.org/word/> which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
J

JGM

Hi Peter,

I guess it's a "phonetic representation" of "nought", no?
Or, as the Americans say, am I way off base?

Cheers!
 
P

Peter Hewett

Hi JGM

Spot on! It's derived from naught (archaic nothing)!

I don't have owt more to say on this topic!

Cheers - Peter
 
M

mcp6453

Charles said:
You might want to consider inserting AutoText rather than inserting an
AutoText field to start with. Then you would not have to unlink (unless the
AT entries, themselves, contain fields).

Suzanne recommended some information on this subject from your web site,
which is why I was wondering whether it is gone forever. I have created
a stationery template using AutoText, but I am inserting the header text
as a field. How would I insert AutoText from normal.dot into another
template unless I insert as a field?
 
C

Charles Kenyon

If you are using vba to insert the AutoText field, you can instead simply
have it insert the AutoText entry. I think inserting a field is harder. What
follows is recorded code inserting an entry. You would want to use the Range
object instead of the selection object in your code, I suspect. Probably the
attached template or a global instead of normal.dot, as well.

NormalTemplate.AutoTextEntries("210 MartinLK").Insert Where:=Selection.
_
Range, RichText:=True


No, my site isn't gone forever, it is just a low priority compared to making
a living. I have it up on another server but haven't gotten around to
changing the address with the Internet.
--

Charles Kenyon

See the MVP FAQ: <URL: http://www.mvps.org/word/> which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top