Checking for RangeShapes in a header

R

Roderick O'Regan

Can anyone nudge me in the right direction with the following problem,
please?

I have a document with odd and even pages.

Every page has a header graphic.

I need to insert a landscape page anywhere after page 1 - this I can
manage OK.

Now I need to go into both the odd and even headers of the landscape
section.

I insert a page break command to display both odd and even pages of
the new section.

Then I go to each of the headers and unlink them from the previous.

Next I need to check if there is a shape range in both the odd and
even headers and delete it. I think I might be able to use the
following commands:
With Selection

..Sections(1).Headers(wdHeaderFooterPrimary).Range.ShapeRange.Select
.Delete

..Sections(1).Headers(wdHeaderFooterEvenPages).Range.ShapeRange.Select
.Delete
End With

And here is the problem:

If, for some reason there isn't a ShapeRange in one of the headers
then one of the commands show above causes Word to crash and therefore
I don't think an On Error statement is appropriate as it's not a
runtime error.

What I am trying to compose is a statement which says "If there is a
ShapeRange in the Primary Header of this section then delete it.
Otherwise, go to the Even Pages of the section header, check if there
is a shape range there and delete it"

Easy to write in pseudo code but I'm baffled on how to write it in
VBA!

Any help on this, please?

Regards

Roderick
 
J

Jezebel

The key to your problem is the use of the Selection. You don't need it. Work
directly with the Ranges themselves. Separately, don't understand your
concern about error-handling. The error caused if the ShapeRange is empty is
indeed a runtime error, which you can trap or ignore.

Simplest would be something like --

On error resume next
With Selection.Sections(1)
.Headers(wdHeaderFooterPrimary).Range.ShapeRange.Delete
.Headers(wdHeaderFooterEvenPages).Range.ShapeRange.Delete
End With

If you really want to check if the ShapeRange is empty, you can use

If .Headers(wdHeaderFooterPrimary).Range.ShapeRange.Count > 0 then ...
 
R

Roderick O'Regan

Thanks Jezebel.

Word was crashing when it encountered the ....ShapeRange.Select
command as shown above. Not the runtime error message box.

However, thanks to your suggestion on deleting the Select and
replacing it with Delete does the trick. No crashing. Shapes are
deleted.

Thanks again.

Regards

Roderick
 

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