none
Powerpoint Shapes.AddPicture() doesn't take selected placeholder into account RRS feed

  • Question

  • Hi

    If you create a master template in PowerPoint containing three image placeholders, and calls Shapes.AddPicture() on that slide PowerPoint automatically groups the image to the first inserted placeholder. This is fine when I don't have any selected placeholders, but  Shapes.AddPicture() doesn't care about if there are any selected placeholders!

    Using the Insert menu in powerpoint when having a placeholder selected, then the image is grouped with that placeholder. 

    Is there a way to do this by code, or is this a bug? Remember that office automatically groups the first available image/media placeholder with the image using Shapes.AddPicture().

    Monday, January 30, 2012 8:29 AM

Answers

  • From the behavior of the method, yes, insert a picture into a certain placeholder is not supported in PIAs. There are two workarounds available for us now: 

    1. Add first and then delete the picture you don't want to appear.

    2. Delete the placeholders which are in the font of the certain placeholder, add picture via calling AddPicture method. At last, calling AddPlaceholder to restore a previously deleted placeholder

    I hope this helps.


    Calvin Gao[MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by MartinMyth Wednesday, February 1, 2012 8:11 AM
    Wednesday, February 1, 2012 6:26 AM
    Moderator

All replies

  • Hi Martin,

    Thanks for your post.

    I'm not sure about this issue, but I write a method which seems to work around the issue, see:

    Sub TestMethod()
        Dim fileName As String
        fileName = "C:\Users\Public\Pictures\Sample Pictures\Lighthouse.jpg"
        Dim pre As Presentation
        Dim sld As Slide
        
        Set pre = ActivePresentation
        Set sld = pre.Slides(5)
        
        'Insert a picture for the third placeholder
        InsertPicForPlaceHolder sld, fileName, 3
        
    End Sub
    
    Sub InsertPicForPlaceHolder(ByVal sld As Slide, ByVal fileName As String, ByVal index As Integer)
        If index > 1 Then
            Dim shapeCollection() As Shape
            ReDim shapeCollection(1 To index)
            Dim s As Shape
        
            For i = 1 To index
                Set s = sld.Shapes.AddPicture(fileName, msoCTrue, msoCTrue, 0, 0)
                Set shapeCollection(i) = s
            Next i
            
            ReDim Preserve shapeCollection(1 To index - 1)
            
            Dim item As Shape
            For i = LBound(shapeCollection) To UBound(shapeCollection)
                Set item = shapeCollection(i)
                item.Delete
            Next i
        Else
            sld.Shapes.AddPicture fileName, msoCTrue, msoCTrue, 0, 0
        End If
    End Sub
    


    Please kindly check this and let me if there is anywhere to improve to correct

    In addition, this issue has been reported before:

    http://social.msdn.microsoft.com/Forums/sr-Latn-CS/vsto/thread/b27a49e7-0282-4912-8b22-e76f058f1e22

    I hope this helps.


    Calvin Gao[MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, January 31, 2012 7:18 AM
    Moderator
  • Hi Calvin, thanks for you time:)

    So what you are proposing, which I have read before, is to populate the placeholders from 1 to the placeholder you want, and delete the previous ones afterwards? Adding a picture, video or sound to a selected placeholder by code is not supported in the Office API?

     

     

     

    Tuesday, January 31, 2012 9:49 AM
  • From the behavior of the method, yes, insert a picture into a certain placeholder is not supported in PIAs. There are two workarounds available for us now: 

    1. Add first and then delete the picture you don't want to appear.

    2. Delete the placeholders which are in the font of the certain placeholder, add picture via calling AddPicture method. At last, calling AddPlaceholder to restore a previously deleted placeholder

    I hope this helps.


    Calvin Gao[MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by MartinMyth Wednesday, February 1, 2012 8:11 AM
    Wednesday, February 1, 2012 6:26 AM
    Moderator