PDA

View Full Version : PowerPoint VBA Determine shp index of selected shape



CitizenMark
09-28-2018, 06:08 AM
Hi,

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

John Wilson
09-28-2018, 11:48 AM
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