Word VBA Using Shapes Question RRS feed

  • Question

  • I've got the following code that I found:

    Sub removeTopAndBottomMostShapesFromActiveDocument()

    Dim shape As shape
    Dim topShape As shape
    Dim bottomShape As shape

    Dim pageNum
    For pageNum = 1 To ActiveWindow.Panes(1).Pages.Count

        Dim highestPoint, lowestPoint
        highestPoint = 999999
        lowestPoint = -999999

        Set topShape = Nothing
        Set bottomShape = Nothing

        Dim sr As ShapeRange
        Set sr =  ActiveWindow.Panes(1).Pages(pageNum).Rectangles.Item(1).Range.ShapeRange
        For Each shape In sr
            If shape.Top < highestPoint Then
                Set topShape = shape
                highestPoint = shape.Top
            End If
            If shape.Top + shape.Height > lowestPoint Then
                Set bottomShape = shape
                lowestPoint = shape.Top + shape.Height
            End If

        If Not topShape Is Nothing Then
        End If
        If Not bottomShape Is Nothing Then
        End If

    With Set sr = Activewindow ... I can't figure out what the .item(1) does.  I've got textboxe in the header and one in the footer of the Word document and they are all the same. On some pages  .item(1) will give an sr.count of "0" but if I change to .item(2) or .item(3)  the textboxes will be found on the specific page. Any help would be  appreciated.

    Friday, August 19, 2016 4:28 PM


  • In your code, .Item(1) refers to the first 'Rectangle' of the page. It's the same as .Rectangles(1).

    Given that the first 'Rectangle' of the page may not contain any Shapes, you need to loop though the Rectangles till you find one that does contain a Shape.

    Paul Edstein
    [MS MVP - Word]

    Friday, August 19, 2016 11:40 PM