VeganWolf
VeganWolf

Reputation: 53

Inserting and Linking a Picture into a Shape Fill using VBA

I am trying to link and insert a picture (*.png) into a shapes fill in powerpoint using vba in Excel. In the end I will loop through this on 100+ pages and the pictures will be updated frequently so automating this will be a huge time saver. Currently I have figured out how to loop through the pages and insert the pictures into the shapes, but I have been unable to figure out how to link the pictures too.

I'm using the below code to fill the shape with the picture but I can't find the syntax to both insert and link it:

Pres.Slides(1).Shapes(ShapeName).Fill.UserPicture PictureFilePath

Ultimately this should behave like clicking on a shape, going format > shape fill > picture > insert and link (On the drop down next to insert in the dialog box).

Upvotes: 1

Views: 5056

Answers (3)

Tarun Reddy
Tarun Reddy

Reputation: 2843

Please find attached code. First create a shape in PPT and run the code. enter image description here

Upvotes: 0

John Korchok
John Korchok

Reputation: 4923

Not all user interface actions are in the VBA object model. One of those exceptions is creating a link to a shape fill. The closest you can get is to link pictures that are inserted as pictures rather than as fills. Here's the syntax to add a linked picture. It assumes the picture is in the same folder as the presentation:

Sub Macro1()
 ActiveWindow.Selection.SlideRange.Shapes.AddPicture(FileName:="Picture1.png", LinkToFile:=msoTrue, SaveWithDocument:=msoFalse, Left:=300, Top:=251, Width:=121, Height:=38).Select
End Sub

Upvotes: 2

Oliver
Oliver

Reputation: 8602

Welcome to SO.

For answering your question, I'll give some credit to this similar SO question based on Excel and this similar SO question based on PP. Some additional information was gathered from the microsoft documentation on the subject.

What you seem to be looking for is the ActionSetting object in the shapes class, which seems to be availible to shapes in PowerPoint. Below is a code snippet created directly in PowerPoint VBA

Sub insertPictureIntoShapeWithLinkToPicture()
    Dim PP_Slide As Slide, PP_Shape As Shape, imagePath As String
    Set PP_Slide = ActivePresentation.Slides(1) 'Set slide (change as necessary)
    Set PP_Shape = PP_Slide.Shapes(1) 'Set shape (change as necessary)
    imagePath = "Path to image"
    With PP_Shape
        'add picutre
        .Fill.UserPicture imagePath
        'Set an action on click
        With .ActionSettings(ppMouseClick)
            'Set action to hyperlink
            .Action = ppActionHyperlink
            'Specify address
            .Hyperlink.Address = imagePath
        End With
    End With
End Sub

The same approach should be available via Excel, either adding a reference to the PowerPoint object library, or using Late-Binding methods. Note that the 'click' method with hyperlink defaults to standard hyperlink clicking (ctrl + left mouse for windows with a Danish keyboard).

Upvotes: 0

Related Questions