Deleting a specific field code in Footers

J

jerem

I would like to use the code below to delete only a certain field code
appearing in each Section's footer. I don't want the page numbering field
codes deleted, I just want the document ID # deleted. When I use the Find
stipulating a field code (^d) it finds any and every field code including
page number field codes. Is there any way to stipulate a specific field code?

The code below works wonderfully for deleting all footers but I want to
replace the
statement of Then oFoot.Range.Delete with something like this: find
specifically the document ID#, which appears in the document in this form: {
DOCPROPERTY "SWDocID" \*MERGEFORMAT }, and then delete it, but I don't know
how to be field code specific.

Any help would be appreciated.

Sub DeleteFooters()
Dim oSec As Section
Dim oFoot As HeaderFooter

For Each oSec In ActiveDocument.Sections
For Each oFoot In oSec.Footers
If oFoot.Exists Then oFoot.Range.Delete
Next oFoot
Next oSec
End Sub
 
L

Lene Fredborg

Below is a revised version of your code. The macro should do what you want.
It checks all fields but only deletes fields of the type DocProperty that
contains the string "SWDocID" in the field code.

Sub DeleteDocPropertyInFooters()
Dim oSec As Section
Dim oFoot As HeaderFooter
Dim oField As Field

For Each oSec In ActiveDocument.Sections

For Each oFoot In oSec.Footers
If oFoot.Exists Then
For Each oField In oFoot.Range.Fields
If oField.Type = wdFieldDocProperty Then
If InStr(1, oField.Code.Text, "SWDocID") > 0 Then
oField.Delete
End If
End If
Next oField
End If
Next oFoot
Next oSec
End Sub

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
H

Helen

Hi jerem, just a note to let you know that I used your For Loop successfully
to answer my question above - updating fields in multiple footers. Thanks
for your post!! :)
cheers, helen
 
J

jerem

Glad to help even though inadvertently. I'd like to take credit for the code
in my original question, but I'd scavenged it myself off of either this
website or another, can't remember. Anyway, there's a quote that has been
credited to Isaac Newton - he said something to the effect that the reason
that he'd been able to accomplish much was because he'd been standing on the
shoulders of giants. Well, I don't know if we can equate the people who
contribute to this forum as giants (after all, Newton was talking about great
minds in physics and mathematics!!), but they're giants in my mind in terms
of how much help I've gotten from them. I've become addicted to this website
and have learned much.

A shout out to everyone out there -- thanks for all your help.

And Helen, keep grazing - you'd be surprised how much you learn from some of
these posts even if you're not experiencing the problem posted. I always
find tidbits here and there that are very useful.
 
J

jerem

Thank you Lene - this works fabulously. Saves me a lot of time. Please see
my reply to Helen above to let you know how much I appreciate your help and
everyone who contributes to this forum.
 
H

Helen

Hi jerem, so sorry about my gender error! I usually try so hard not to make
assumptions, and here I've gone and assumed jerem is short for jeremy!
Anyway, I know what you mean about the shoulders of others, and I have also
learned sooo much from the kind folk here, the MVPs site and others...
Likewise a big thank you to everyone!!
Thanks again and my best wishes to you, jerem :)
 
D

Dorak

This is exactly code I was looking for too. But I have a question:
After the field is deleted two returns are left in the footer.
I've been fiddling to no avail with how to get rid of them in footers.
Any help will be greatly appreciated!
 
C

celia

Hi,
I'm having this problem.
DOCPROPERTY"WFWProfile" \* MERGEFORMAT 1Error! Bookmark not defined.

Wanted to delete it but can't.

What am i suppose to do?

Thanks.
 
D

Doug Robbins - Word MVP

Can we have some more information about what you are trying to do? What you
have tried? What happens when you select it and press delete?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 

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