Results 1 to 2 of 2

Thread: PowerPoint VBA Determine shp index of selected shape

  1. #1

    PowerPoint VBA Determine shp index of selected shape


    how does one determine the shape index of the currently selected shape in powerpoint with VBA?

    I have spent an hour looking for an answer to what I thought would be a straightforward problem, including reading up on shapes and their methods and properties but a solution has escaped me. I apologize if this answer is out there, but none of my queries have yielded any answer other than how to solve the querier's problem by not using the shape's index.

    I need to go through a slide deck and crop only the selected picture on the current slide that I am working on.
    There is no systematic ordering or naming of the images on individual slides.
    There may be multiple images of which I may wish to crop one or two but not all.
    So I believe that I need a macro that will only act on the currently selected picture.

    I cobbled together the following code which works with a hard coded shape index for the specific slide I have been experimenting on....

    Sub CropSelectedImage()

    CurrentSlide = ActiveWindow.View.Slide.SlideNumber
    Set myDocument = ActivePresentation.Slides(CurrentSlide)

    'Hard coded version works but
    current_shape_index = 4 '<<<<<<< need code here to Get Selected Shape Index

    myDocument.Shapes(current_shape_index).PictureFormat.CropTop = 20

    End Sub

    Thank you - Mark

  2. #2
    VBAX Master
    Feb 2007
    Like this?

    Sub cropMe()
    Dim oshp As Shape
    On Error Resume Next
    Err = 0
    Set oshp = ActiveWindow.Selection.ShapeRange(1)
    If Err = 0 Then
    If isPicture(oshp) Then
    oshp.PictureFormat.CropTop = 20
    End If
    End If
    End Sub
    Function isPicture(oshp As Shape) As Boolean
    If oshp.Type = msoPicture Then isPicture = True
    If oshp.Type = msoPlaceholder Then
    If oshp.PlaceholderFormat.ContainedType = msoPicture Then isPicture = True
    End If
    End Function
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials

Posting Permissions

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