select part of the slide in powerpoint VBA

2019-08-31 00:44发布

问题:

is there a way to select a specific area of the slide from VBA by giving the coordinates of the area to be selected. EG I want to select an area with position top=100, left=100, hight=10 and width=10, and this area includes 3 text boxes that I want to manipulate somehow afterwords, say align left all 3 of them.

Particualrly, I have many slides with those 3 boxes, and then some other boxes as well, where the position of those 3 varies +/- 0.3cm from one slide to another, and now I want them to have the same position on all slides. The problem is that I some of them are empty boxes so that I don't know how to search for for them and then include them in the selection, since they don't have the same shape index on all slides. Therefore I thought if there is a code to select a particular area of the slide - that would pretty much solve my problem...

Thnx!!!

回答1:

The FindShapesInSlides code will select all of the shapes (textboxes, etc) in any slide in the slideshow that match the specified boundary (based on top, left, height, width).

You can then add whatever actions you would like to the selected items (like aligning them or whatever).

Select all shapes within a specified boundary (top, left, height, width)

Sub FindShapesInSlides(pTop As Single, pLeft As Single, pHeight As Single, pWidth As Single)

    Dim slideShapes As Shapes
    Dim slideShape As Shape

    'Loop through each slide in the presentation
    For i = 1 To ActivePresentation.Slides.Count

        'Get shapes for the slide
        Set slideShapes = ActivePresentation.Slides(i).Shapes

        'Remove any existing selection
        ActiveWindow.Selection.Unselect

        'Loop through the shapes
        For Each slideShape In slideShapes

            'Check if shape is within the specified boundary
            If ((slideShape.top >= pTop And slideShape.top <= (pTop + pHeight)) _
                And (slideShape.left >= pLeft And slideShape.left <= (pLeft + pWidth))) Then

                'Add the shape to the current selection
                slideShape.Select (msoFalse)

            End If
        Next slideShape
    Next i

End Sub

Testing the selection

Sub Test()

    FindShapesInSlides pTop:=50, pLeft:=50, pHeight:=100, pWidth:=50

End Sub