How do I change Image via VBA

0

Hello, I would like to change an image in Power Point, I do not want to delete and insert another , but rather do what the command indicated in the image does. Would anyone know how to call this method ???

    
asked by anonymous 07.11.2017 / 12:52

2 answers

1

The Change Image is not exposed in the object , 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

Test

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 of 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 of 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
    
04.09.2018 / 14:35
0

Good morning Rodolfo

The command to call this function in VBA is

Application.Dialogs(xlDialogInsertPicture).Show

It serves both Powerpoint and Excel and Word.

    
22.11.2017 / 13:26