PDA

View Full Version : Reference to Sections...Footers(wdHeaderFooterFirstPage).Range.ShapeRange(1).Select



Mavila
08-05-2015, 01:55 PM
Hi, I've run into a puzzling result when trying to select a shape in a section's footer. It select's the wrong section's footer shape.

My code is:
ActiveDocument.Sections(ActiveDocument.Sections.Count).Footers(wdHeaderFoot erFirstPage).Range.ShapeRange(1).Select

The code creates a page, inserts content and then creates a section break (next page) and repeats (creates content in the next section and so on). Each page has a footer which is a rectangle shape that contains a table with a title for the section, page numbering and some other data.

It works fine for the first section, but when I run the above code on the second section, it selects the shape on the first page of the first section - not the first page shape in the second section.

I do this a couple of different ways depending on the type of document selected by the user. I don't encounter this problem when I run it on a document with the portrait orientation, but it happens when I run it on a landscape orientation - could that account for the problem?

I've inserted, msgbox Activedocument.sections.count and it correctly returns 2, but the above code still selects the first page footer shape in section #1.

Any ideas what might be going on here?

gmayor
08-05-2015, 09:32 PM
I would guess that this is Word 2013 and appears to be a bug in that version. I have not yet found a workaround.
It works fine in Word 2010 (still the best of the Word versions in my opinion).

Mavila
08-05-2015, 10:05 PM
I would guess that this is Word 2013 and appears to be a bug in that version. I have not yet found a workaround.
It works fine in Word 2010 (still the best of the Word versions in my opinion).

Ouch.

Yes, it's 2013.

gmayor
08-06-2015, 09:21 PM
This has been bugging me. I have found a belt and braces method of selecting the appropriate shape that does work in Word 2013. Do with it as you will.


Sub Example()
Dim oStory As Range
Dim oFooter As Range
Dim oShape As Shape
Set oFooter = ActiveDocument.Sections.Last.Footers(wdHeaderFooterFirstPage).Range
For Each oStory In ActiveDocument.StoryRanges
For Each oShape In oStory.ShapeRange
oShape.Select
If Selection.Range.InRange(oFooter) Then
GoTo lbl_Exit
End If
Next oShape
If oStory.StoryType <> wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
For Each oShape In oStory.ShapeRange
oShape.Select
If Selection.Range.InRange(oFooter) Then
GoTo lbl_Exit
End If
Next oShape
Wend
End If
Next oStory
lbl_Exit:
Application.ScreenRefresh
Set oStory = Nothing
Set oFooter = Nothing
Set oShape = Nothing
Exit Sub
End Sub

Mavila
08-07-2015, 06:49 AM
OK, thanks. I will try it out.

Mavila
08-07-2015, 11:29 AM
I wonder if switching the orientation after the fact would be more efficient?

gmayor
08-07-2015, 08:41 PM
The problem is not related to orientation as far as I can see.

Mavila
08-12-2015, 04:58 PM
It works fine when I'm in portrait orientation.

I'm going to try to switch orientation afterward and see what happens.