Results 1 to 4 of 4
  1. #1
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Shapes in Headers (Word 02 SR1)

    I want my VBA code to count and subsequently select all of the Shapes (text boxes, etc.) in my document. I'm able to do this in the main document, but not in headers and footers. How would I access the Shapes Collection in headers and footers?
    Thanks,
    Richard Barrett

  2. #2
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Shapes in Headers (Word 02 SR1)

    You want something like the attached.
    <pre>Public Sub HdrFtrShapes()
    Dim rngStory As Range
    For Each rngStory In ActiveDocument.StoryRanges
    Select Case rngStory.StoryType
    Case wdEvenPagesFooterStory, wdEvenPagesHeaderStory, _
    wdFirstPageFooterStory, wdFirstPageHeaderStory, _
    wdPrimaryFooterStory, wdPrimaryHeaderStory
    MsgBox rngStory.InlineShapes.Count
    MsgBox rngStory.ShapeRange.Count
    End Select
    Next rngStory
    End Sub
    </pre>

    I get errors from rngStory.ShapeRange.Count when there are no shapes in the header. I'm not sure why!


    StuartR

  3. #3
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Shapes in Headers (Word 02 SR1)

    This slightly longer macro doesn't generate errors on empty headers.

    I assume you don't want to count shapes in headers and footers that are linked to the previous one, if you want to count these shapes for every header then remove the If and End If lines.

    I haven't tried to select the shapes for you, because you could only have the shapes from one header or footer selected at a time.

    <pre>Sub HdrShapes()
    Dim secSection As Section
    Dim hfHeader As HeaderFooter
    Dim hfFooter As HeaderFooter

    For Each secSection In ActiveDocument.Sections
    For Each hfHeader In secSection.Headers
    If Not hfHeader.LinkToPrevious Then
    MsgBox hfHeader.Shapes.Count
    MsgBox hfHeader.Range.InlineShapes.Count
    End If
    Next hfHeader
    For Each hfFooter In secSection.Footers
    If Not hfFooter.LinkToPrevious Then
    MsgBox hfFooter.Shapes.Count
    MsgBox hfFooter.Range.InlineShapes.Count
    End If
    Next hfFooter
    Next secSection
    End Sub
    </pre>


  4. #4
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: Shapes in Headers (Word 02 SR1)

    Thank you very much. This will help me.
    Richard

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •