none
Powerpoint - Inserting picture outside of placeholder RRS feed

  • Question

  • Hi,

    I'm trying to get a picture to insert on the active powerpoint slide without using up a placeholder. I saw a suggestion saying to insert (N+1) of the pictures on the slide, where N is the amount of placeholders on the slide and then give these images some sort of identified so you can delete them. I have the inserting N+1 images working, I then give all but one of them a name "DELETEME" and go through and try to delete all shapes with that name. This works for slides with 0 or 1 placeholders, but with 2 placeholders it fails. It inserts 3 pictures correctly, but only deletes 1 of the pictures, meaning two remain (one on its own and one connected to a placeholder).

    Code:

                    Powerpoint.Shape sh;

     

                    // inserts (N+1) pictures, where N is the amount of placeholders

                    do

                    {

                        sh = slide.Shapes.AddPicture(logo, MsoTriState.msoFalse, MsoTriState.msoTrue, slideWidth / 2, slideHeight / 2);

                        sh.Name = "DELETEME";

                    }

                    while (sh.Type != MsoShapeType.msoPicture);

     

                    // Remove "DELETEME" from the (N+1)th shapes name

                    sh.Name = "";

     

                    Powerpoint.Shapes shapes = slide.Shapes;

     

                    foreach (Powerpoint.Shape shape in shapes)

                    {

                        if (shape.Name == "DELETEME")

                            shape.Delete();

                    }

     

    I cant for the life of me work out why its not working for 2 placeholders

    Sunday, December 18, 2011 2:58 AM

Answers

  • Hi,

    I think that the iterator will be not rebuilded after Delete methode in foreach loop.
    You can try this code:
    for(int iLoop=1; iLoop<=objSlide.Shapes.Count;iLoop++)
    {
        shape= objSlide.Shapes[iLoop];
        if (shape.Name == "DELETEME")
        {
                   shape.Delete();
                   iLoop--;
        }
    }

     

    Regards,

    Iso

     

    • Marked as answer by ruggider Monday, December 19, 2011 9:47 PM
    Monday, December 19, 2011 11:22 AM

All replies

  • Hi ruggider,

     

    Thanks for posting in the MSDN Forum.

     

    Would you please clarify some questions:

     

    1.         Are the “placeholders” PowerPoint Shape instances?

    2.         Is the snippet which you show me is in the same class? If it is, there has some conflict in the snippet:

    a)         You declare a PowerPoint.Shape variable to point a picture at fist and you do … while circle will not do other things. I’m confused why you use this circle in that place.

    b)         Next you change the particular Shape’s name to an empty string.

    c)         Then you iterate the Shapes collection to delete the shape which name “DELETEME”. As far as we know you changed the shape’s name to an empty string at step b.

     

    It’s based on my experience that snippet will insert picture at the same place without number limited. However I would remind you that all of the pictures have same position on the slide, you will not see others if you don’t move the pictures to different positions manually.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Monday, December 19, 2011 5:29 AM
    Moderator
  • Hi,

    The placeholders are literal placeholders, these ones: http://office.microsoft.com/en-gb/powerpoint-help/add-a-placeholder-to-a-layout-HA010078895.aspx

    This is all one class. I've found the do...while written above inserts (N+1) pictures (where N is the amount of placeholders).

    The reason is if you insert a picture, and that picture is put in a placeholder, the pictures type is actually not a picture shape. So in the case where there are two placeholders it will insert 3 pictures since only the 3rd one's type is a picture.

    So this code:

    Inserts picture 1 - Name = DELETEME (type is not picture)

    Inserts picture 2 - Name = DELETEME (type is not picture)

    Inserts picture 3 - Name = DELETEME (type is picture), then sets Name = nothing for this picture

     

    So when I loop over all shapes, it should delete picture 1 and 2 but not 3. Instead what it does is delete one picture but leaves two remaining, one tied to the 2nd placeholder on the slide, and one free floating. If I comment out the delete loop and run it, there are 3 pictures on the slide, two are tied to placeholders and one is free floating.

     

    Hopefully that makes sense. I think it might be easier to just run the code to see the behaviour.

     

    Monday, December 19, 2011 5:40 AM
  • Hi,

    I think that the iterator will be not rebuilded after Delete methode in foreach loop.
    You can try this code:
    for(int iLoop=1; iLoop<=objSlide.Shapes.Count;iLoop++)
    {
        shape= objSlide.Shapes[iLoop];
        if (shape.Name == "DELETEME")
        {
                   shape.Delete();
                   iLoop--;
        }
    }

     

    Regards,

    Iso

     

    • Marked as answer by ruggider Monday, December 19, 2011 9:47 PM
    Monday, December 19, 2011 11:22 AM
  • Thanks, it works perfectly now!
    Monday, December 19, 2011 9:47 PM