Esa
Esa

Reputation: 1666

PowerPoint/VBA: How to replace placeholder with image when slide loads

I trying to make PowerPoint load up images to replace placeholders everytime a slide changes. I have the code working, which changes the placeholders with images from local drive or url. But it wont work on OnSlideShowPageChange() event(mentioned here). With no prior experience on VB/VBA, I have no idea why, as it does not give any errors. I know the event is accessed because if I put a MsgBox()-function in it, it is displayed.

ImageReplace code:

Dim strPicName As String
Dim shp As Shape
Dim sglShapeLeft As Single
Dim sglShapeTop As Single
Dim sglShapeHeight As Single
Dim sglShapeWidth As Single

'Get the name of the shape (image)
'Provided this is the only shape on the slide
'Since I don't think you can use the ME. keyword to reference an impage from Powerpoint VBA
'(Me.shape.Name)
For Each shp In ActiveWindow.Selection.SlideRange.Shapes
    strPicName = shp.Name
Next shp

'Select the Image
ActiveWindow.Selection.SlideRange.Shapes(strPicName).Select
'Get the Left and Top starting points and width and height
sglShapeLeft = ActiveWindow.Selection.SlideRange.Shapes(strPicName).Left
sglShapeTop = ActiveWindow.Selection.SlideRange.Shapes(strPicName).Top
sglShapeHeight = ActiveWindow.Selection.SlideRange.Shapes(strPicName).Height
sglShapeWidth = ActiveWindow.Selection.SlideRange.Shapes(strPicName).Width
'Delete the Image
ActiveWindow.Selection.ShapeRange.Delete
'Insert a new Image at the same starting points as the previous image
ActiveWindow.Selection.SlideRange.Shapes.AddPicture(FileName:="<picturePath/url>", LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=sglShapeLeft, Top:=sglShapeTop, Width:=sglShapeWidth, Height:=sglShapeHeight).Select

For Each shp In ActiveWindow.Selection.SlideRange.Shapes
    strPicName = shp.Name
Next shp

ActiveWindow.Selection.SlideRange.Shapes(strPicName).IncrementRotation 276#

Any help is appreciated

Upvotes: 0

Views: 7148

Answers (1)

Paul B.
Paul B.

Reputation: 2524

ActiveWindow is not accessible when in slide show view.

Try this instead

Dim sld As Slide
Set sld = ActivePresentation.Slides _
    (ActivePresentation.SlideShowWindow.View _
    .CurrentShowPosition)

Set shp = sld.Shapes(1)

With shp
    sld.Shapes.AddPicture(FileName:="<picturePath/url>", LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=.Left, Top:=.Top, Width:=.Width, Height:=.Height).IncrementRotation 276#
    .Delete
End With

BTW, debugging and exceptions do not seem to be supported in the OnSlideShowPageChange event. As an easy approach place a MsgBox after each line of code to see where the execution stops.

Upvotes: 1

Related Questions