deleting shapes in headers?

C

Chuck

Hi all

This is driving me mad -- in Word 2000...

I'm trying to delete logos from headers from one section at a time. When I
use the following code, even though I'm specifying the Shapes in a specific
section's headers, all the Shapes in all the headers are deleted:

Dim aheader As HeaderFooter
Dim myshape As Shape

For Each aheader In ActiveDocument.Sections(2).Headers

For Each myshape In aheader.Shapes
myshape.Delete
Next myshape

Next aheader

(The headers are not LinkedToPrevious.)

Word VBA help says the following about the Shapes Collection Object:

"To count the shapes in all the headers and footers, use the Shapes
collection with any HeaderFooter object."

This seems to imply that Shapes in headers/footers are somehow linked
regardless of whether the headers/footers are linked and that might explain
why using myshape.delete to delete one Shape would delete all Shapes.
However, I can manually delete individual Shapes (logos) from specific
section headers without affecting any other sections.

I've tried converting the Shapes to InlineShapes and then deleting the
InlineShapes (adapting the same code above but declaring myshape as
InlineShape) but the converted InlineShapes don't delete.

Any ideas about how to achieve the goal of getting rid of those logos from
specific section headers only without affecting any other headers?
Suggestions gratefully appreciated!

Chuck
 
P

Peter Hewett

Hi Chuck

It's quite probable that the shapes are InlineShapes. Try this code, you can also add
floating shapes (Shape objects) to the code intead/as well if necessary:

Public Sub DeleteInlineShapesInHeader(ByVal Section As Integer)
Dim hfItem As Word.HeaderFooter
Dim ilsItem As Word.InlineShape

' Section must be valid
If Section > 0 And Section <= ActiveDocument.Sections.Count Then

' Enumerate all sections
For Each hfItem In ActiveDocument.Sections(Section).Headers

' Enumerate the inline shapes collection and delete all shapes
For Each ilsItem In hfItem.Range.InlineShapes
ilsItem.Delete
Next
Next
End If
End Sub ' DeleteInlineShapesInHeader

Call it like this:

DeleteInlineShapesInHeader(2)

HTH + Cheers - Peter


Hi all

This is driving me mad -- in Word 2000...

I'm trying to delete logos from headers from one section at a time. When I
use the following code, even though I'm specifying the Shapes in a specific
section's headers, all the Shapes in all the headers are deleted:

Dim aheader As HeaderFooter
Dim myshape As Shape

For Each aheader In ActiveDocument.Sections(2).Headers

For Each myshape In aheader.Shapes
myshape.Delete
Next myshape

Next aheader

(The headers are not LinkedToPrevious.)

Word VBA help says the following about the Shapes Collection Object:

"To count the shapes in all the headers and footers, use the Shapes
collection with any HeaderFooter object."

This seems to imply that Shapes in headers/footers are somehow linked
regardless of whether the headers/footers are linked and that might explain
why using myshape.delete to delete one Shape would delete all Shapes.
However, I can manually delete individual Shapes (logos) from specific
section headers without affecting any other sections.

I've tried converting the Shapes to InlineShapes and then deleting the
InlineShapes (adapting the same code above but declaring myshape as
InlineShape) but the converted InlineShapes don't delete.

Any ideas about how to achieve the goal of getting rid of those logos from
specific section headers only without affecting any other headers?
Suggestions gratefully appreciated!

Chuck

HTH + Cheers - Peter
 
P

Peter Hewett

Hi Chuck

I always name shapes out of habit, as you've discovered it's easier to find the exact one
you want. But I don't see why you should need to delete shapes by name. All that's doing
is providing an alternate index to the Shapes collection. When deleting items from a
collection the best practice is to enumerate the collect from end to start and not from
start to end. For interests sake you may want to try this approach (then again you might
not!).

When you enumerate a collection deleting objects from it, often the enumerator gets
screwed up when looping from front to back, hence the reverse order approach.

HTH + Cheers - Peter
 

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