delete frames in word doc based on content?

C

chuck

Hi ther

I'm trying to write a sub that will go through all the footers in a document looking for frames that contain date fields, then delete those frames only. The sub needs to be able loop through multiple sections, some with different first page footers

I understand looping -- but I'm having trouble getting my head around how to efficiently find and delete only the frames that contain date fields (I can find and delete both frames and fields separately)

Would it be best to search for date fields and then test to see if they're contained in frames? If so, what is the property I should identify/test for? Testing for the "Parent" of the field seems to return "Document"

Or should I loop through the frames in each footer, search within the frames for date fields, and if found, then select and delete the frame? If so, any tips on what the best way to do that might be

Any help greatly appreciated

Chuck
 
D

Doug Robbins - Word MVP

I haven't tried this, but I think it should work:

Dim aframe As Frame, afooter As HeaderFooter, afield As Field
For i = 1 To ActiveDocument.Sections.Count
For Each afooter In ActiveDocument.Sections(i).Footers
For Each aframe In afooter.Range.Frames
For Each afield In aframe.Range.Fields
If afield.Type = wdFieldDate Then
aframe.Delete
Exit For
End If
Next afield
Next aframe
Next afooter
Next i

Note that there may be other types of date fields ( wdFieldCreateDate,
wdFieldSaveDate, wdFieldPrintDate) with which you may also need to deal.

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
chuck said:
Hi there

I'm trying to write a sub that will go through all the footers in a
document looking for frames that contain date fields, then delete those
frames only. The sub needs to be able loop through multiple sections, some
with different first page footers.
I understand looping -- but I'm having trouble getting my head around how
to efficiently find and delete only the frames that contain date fields (I
can find and delete both frames and fields separately).
Would it be best to search for date fields and then test to see if they're
contained in frames? If so, what is the property I should identify/test
for? Testing for the "Parent" of the field seems to return "Document".
Or should I loop through the frames in each footer, search within the
frames for date fields, and if found, then select and delete the frame? If
so, any tips on what the best way to do that might be?
 
C

Chuck

Thanks Doug. I tried the code you suggested and it worked except that it deleted the frame but not the contents of the frame. I amended your code slightly to select the frame and then delete the selection (see below with changed lines marked '*change*) and that deletes the frames and their contents. But it takes the document into "normal" view rather than staying in "print" view. Any ideas why the view would change?

Dim aframe As Frame, afooter As HeaderFooter, afield As Fiel
For i = 1 To ActiveDocument.Sections.Coun
For Each afooter In ActiveDocument.Sections(i).Footer
For Each aframe In afooter.Range.Frame
For Each afield In aframe.Range.Field
If afield.Type = wdFieldDate The
aframe.Select '*change
Selection.Delete '*change
Exit Fo
End I
Next afiel
Next afram
Next afoote
Next i
 
D

Doug Robbins - Word MVP

Hi Chuck,

Use

Dim aframe As Frame, afooter As HeaderFooter, afield As Field
For i = 1 To ActiveDocument.Sections.Count
For Each afooter In ActiveDocument.Sections(i).Footers
For Each aframe In afooter.Range.Frames
For Each afield In aframe.Range.Fields
If afield.Type = wdFieldDate Then
afield.Delete
aframe.Delete
Exit For
End If
Next afield
Next aframe
Next afooter
Next i

or

Dim aframe As Frame, afooter As HeaderFooter, afield As Field
For i = 1 To ActiveDocument.Sections.Count
For Each afooter In ActiveDocument.Sections(i).Footers
For Each aframe In afooter.Range.Frames
For Each afield In aframe.Range.Fields
If afield.Type = wdFieldDate Then
aframe.Range.Delete
Exit For
End If
Next afield
Next aframe
Next afooter
Next i


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
Chuck said:
Thanks Doug. I tried the code you suggested and it worked except that it
deleted the frame but not the contents of the frame. I amended your code
slightly to select the frame and then delete the selection (see below with
changed lines marked '*change*) and that deletes the frames and their
contents. But it takes the document into "normal" view rather than staying
in "print" view. Any ideas why the view would change?
 
D

Doug Robbins - Word MVP

Hi Chuck,

It's always better to work with the .Range object when you can rather than
the . Selection object as with the former, the screen does not have to be
refreshed to show the selection so things run faster.
--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
Chuck said:
Hi Doug

I tried your second suggestion (aframe.range.delete) which deleted the
contents of the frames but not the frames. I didn't try the first
suggestion (delete field then frame) because there's text as well as a field
contained in the frame.
As an alternative range.delete approach i tried the following which
deleted the frames and their contents:
For i = 1 To ActiveDocument.Sections.Count
For Each afooter In ActiveDocument.Sections(i).Footers
For Each aframe In afooter.Range.Frames
For Each afield In aframe.Range.Fields
If afield.Type = wdFieldDate Then
Set myrange = aframe.Range *change*
myrange.Delete *change*
Exit For
End If
Next afield
Next aframe
Next afooter
Next i

That seems to be the same number of steps as the
aframe.select/selection.delete approach I tried yesterday. Any thoughts on
whether one or the other approaches (ie deleting a selection vs range) has
any advantages/disadvantages in general?
 

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