0
Change Image is not exposed in object model, then a User Defined Function (UDF) can be created to solve this problem with a custom function.
Function
Function pseudo_alterar_imagem(caminho As String)
'John SR Wilson
'https://answers.microsoft.com/en-us/msoffice/forum/msoffice_powerpoint-mso_winother-mso_2007/how-do-i-make-vba-use-the-change-picture-function/dbf7c8cc-8168-4afd-9336-54a50ebda74e
On Error GoTo err
Dim oImg As Shape
Dim osld As Slide
Dim novaImg As Shape
Dim ehImg As Boolean
Dim ehAlto As Boolean
Set oImg = ActiveWindow.Selection.ShapeRange(1)
Set osld = oImg.Parent
If oImg.Type = msoPicture Or oImg.Type = msoLinkedPicture Then ehImg = True
If oImg.Type = msoPlaceholder Then
If oImg.PlaceholderFormat.ContainedType = msoPicture Or oImg.PlaceholderFormat.ContainedType = msoLinkedPicture Then
ehImg = True
End If
End If
If Not ehImg Then
err.Raise Number:=vbObjectError + 1000, Description:="Seleção não é imagem"
Exit Function
End If
If oImg.Height >= oImg.Width Then ehAlto = True
Set novaImg = osld.Shapes.AddPicture(caminho, msoFalse, msoTrue, 0, 0, -1, -1)
novaImg.LockAspectRatio = True 'should already be set but worth checking
If novaImg.Height >= novaImg.Width And ehAlto = True Then
novaImg.Height = oImg.Height
novaImg.Top = oImg.Top
novaImg.Left = oImg.Left + oImg.Width / 2 - novaImg.Width / 2
Else
novaImg.Width = oImg.Width
novaImg.Left = oImg.Left
novaImg.Top = oImg.Top + oImg.Height / 2 - novaImg.Height / 2
End If
oImg.Delete
Exit Function
err:
MsgBox err.Description
End Function
Testing
Where the desired image should be selected.
Sub teste()
pseudo_alterar_imagem ("C:\Pasta de Testes\imagem.jpeg")
End Sub
If you want to select an image from a specific Slide
In this example Slide 1
Sub teste()
For Each sh In ActivePresentation.Slides(1).Shapes
If sh.Type = msoPicture Or sh.Type = msoLinkedPicture Then sh.Select
Next sh
pseudo_alterar_imagem ("C:\Pasta de Testes\imagem.jpeg")
End Sub
If you want to select an image from the Active Slide
Sub teste()
For Each sh In ActivePresentation.Slides(ActiveWindow.View.Slide.SlideNumber).Shapes
If sh.Type = msoPicture Or sh.Type = msoLinkedPicture Then sh.Select
Next sh
pseudo_alterar_imagem ("C:\Pasta de Testes\imagem.jpeg")
End Sub
I could not do this in Power Point 1- "xlDialogInsertPicture" is only for Excel; 2- the ". Dialogs" method does not exist in Powerpoint.
– Rodolfo Rocha Dos Santos