PDA

View Full Version : [SOLVED:] Shapes 'n Headers 'n Footers



Mavila
10-07-2016, 01:08 PM
I'm trying to isolate a specific shape in a specific header to work with it. I have this bit of code:



Dim shapesCount As Long
shapesCount = ActiveDocument.Sections(ActiveDocument.Sections.count).Headers(wdHeaderFoot erFirstPage).Shapes.count
If shapesCount > 0 Then
ActiveDocument.Sections(ActiveDocument.Sections.count).Headers(wdHeaderFoot erFirstPage).Shapes(a).Select
End IF



I'm assuming this would select the last section's first page header. However, shapesCount is the total number of shapes in the document (it equals 8, rather than 1).

Any reason why that would be?

gmaxey
10-07-2016, 04:39 PM
It is not all shapes in the document rather all shapes in the "Headers\Footer" storyranges. That is just the way it is. You may be able to get the shape you are after using its anchor:


Sub ScratchMacro()
Dim oShape As Shape
'A basic Word macro coded by Greg Maxey
For Each oShape In ActiveDocument.Sections(ActiveDocument.Sections.Count).Headers(1).Shapes
If oShape.Anchor.InRange(ActiveDocument.Sections(ActiveDocument.Sections.Count ).Headers(wdHeaderFooterFirstPage).Range) Then
oShape.TextFrame.TextRange = "Here I am!!"
End If
Next oShape
lbl_Exit:
Exit Sub
End Sub

Mavila
10-13-2016, 09:59 AM
Hmmm, I have to loop through all the shapes to find the one I want to work with? That's a bummer.

gmaxey
10-13-2016, 10:28 AM
Actually you only have to loop until the requested shape is found then exit the loop:


Sub ScratchMacro()
Dim oShape As Shape
'A basic Word macro coded by Greg Maxey
For Each oShape In ActiveDocument.Sections(ActiveDocument.Sections.Count).Headers(1).Shapes
If oShape.Anchor.InRange(ActiveDocument.Sections(ActiveDocument.Sections.Count ).Headers(wdHeaderFooterFirstPage).Range) Then
oShape.TextFrame.TextRange = "Here I am!!"
Exit For
End If
Next oShape
lbl_Exit:
Exit Sub
End Sub

gmaxey
10-13-2016, 10:51 AM
If that shape is static and you work with it frequently, you could name it and then get it by name:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Selection.ShapeRange(1).Name = "My Shape"
lbl_Exit:
Exit Sub
End Sub
Sub Test()
Dim shapesCount As Long
shapesCount = ActiveDocument.Sections(ActiveDocument.Sections.Count).Headers(wdHeaderFoot erFirstPage).Shapes.Count
If shapesCount > 0 Then
ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Shapes("My Shape").Select
End If
End Sub

Mavila
10-13-2016, 04:59 PM
I'm trying to spare you a lot of unnecessary reading, Greg.

What I'm doing is enabling users to create a planning book that changes from project to project in terms of the content of the book. So, on one project you might include chapters a-g, l-z, and another would include different chapters. I delineate each chapter by a section break (turn off previous).

When I create a new chapter, I create a FirstPage Header and Footer and a Primary Page Header and Footer. When the code inserts a section break, those headers/footers are repeated and I want to isolate the headers/footers on this new chapter/section and work with them. Then, I proceed to the next chapter/section and so on. It can be many chapters that I would rather not loop through.

So, I like the idea of naming the shape when it's created and then isolating it and working with it. Then setting the name to "" when the next chapter is created - does that sound like something workable?

I assume each new shape created is "...shapes.count + 1", so I suppose it could be referenced by number in that way (count - #), or maybe the naming method is the way to go. Name each one and then reset the names to "" for the next chapter.

Thank you for your insight.

gmaxey
10-13-2016, 05:11 PM
I don't completely follow what you are trying to do, but I suppose that as you create a shape you could give it a name like "MyShape" & ActiveDocument.Sections.Count. Then get it as explained above.

Mavila
10-13-2016, 06:23 PM
I think that might work. Thank you for your time and advice.