none
PowerPoint C# VSTO - Adding shapes are added as MsoPlaceHolder RRS feed

  • Question

  • Hi all,
    I've been working on an Office 2010 VSTO add-in using C#, and sometimes - when I'm adding a new shape to a slide programmaticly, say - a picture (using 'Shapes.AddPicture(...)'), it's being added as a 'MsoShapeType.msoPlaceholder' and the picture itself is added to the presentation incompletely (e.g. a small portion of the picture is transparent).

    Anyone knows what could be the cause of this?
    Monday, April 2, 2012 7:42 AM

Answers

  • Hi Shay.livyatan,

    As far as I know when you insert image from slide.Shapes.AddPicture will not insert image to Placeholder which has already existed. any images will have placeholder for themselves. I think might the exist placeholder effect your issue. So you work round seems only way to solve it.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Shay.livyatan Thursday, April 5, 2012 6:55 AM
    Thursday, April 5, 2012 4:46 AM
    Moderator

All replies

  • Hi Shay.livyatan,

    Thanks for posting in the MSDN Forum.

    Would you please clarify the mean of "and the picture itself is added to presentation incompletely (e.g. a small portion of the picture is transparent)"?

    As far as I know, the Shapes.AddPicture doesn't have a MsoShapeType parameter. The following snippet will add a picture to a slide:

            private void button1_Click(object sender, RibbonControlEventArgs e)
            {
                PowerPoint.Presentation p = Globals.ThisAddIn.Application.ActivePresentation;
                PowerPoint.Slide s = p.Slides[1];
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "PNG|*.png|JPEG|*.jpg;*.jpeg|GIF|*.gif|All File|*.*";
                ofd.ShowDialog();
                s.Shapes.AddPicture(ofd.FileName,
                    Microsoft.Office.Core.MsoTriState.msoFalse,
                    Microsoft.Office.Core.MsoTriState.msoCTrue, 0, 0);
            }

    I hope it can help you.
    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, April 3, 2012 3:03 AM
    Moderator
  • Hi Tom, and thank you for replying.

    I'll try to clarify what I mean by "incomplete picture" with the following screen capture from such an occurrence:


    As you can see, the image (which is the chart) is added to the slide, but a small portion of its bottom is missing (you can see that the picture container is in the actual size of the picture, but it seems as if it "forgets" to draw the picture to the end).

    This happens around 90% of the time (in case of those other 10% - the picture is drawn entirely), when I use this code to add the picture to a slide:

    Slide activeSlide = Globals.ThisAddIn.Application.ActiveWindow.View.Slide; float position = Globals.ThisAddIn.GetPositionIterator(); Microsoft.Office.Interop.PowerPoint.Shape ppPicture = activeSlide.Shapes.AddPicture(fileName, MsoTriState.msoTrue, MsoTriState.msoTrue, position, position);

    ppPicture.Name = pictureName;
    ppPicture.LinkFormat.SourceFullName = urlText.Text;

    As you can see, I'm using a linked picture - if that matters...

    What I mean by that the shape is added as 'MsoShapeType.msoPlaceholder', is that although I can clearly see while debugging right after the ".AddPicture()" line, that the shape's type is indeed of 'MsoShapeType.msoLinkedPicture', in another class - I have a method which iterates between all the shapes in the presentation and looking for shapes of type 'MsoShapeType.msoLinkedPicture', sometimes (not all the time) those pictures that I add are found by the method but they're typed as 'MsoShapeType.msoPlaceholder'  - even though, while debugging, I can clearly see that the shape is my linked picture (e.g. it has the ".LinkFormat.SourceFullName" member which points specifically to the URL I used to create it).

    This might be useful - the picture itself is taken from a REST service: I download the picture, save it in the %TEMPDIR% of the computer, and then add it to the slide. The picture that I download in this manner is downloaded completely, so if I just open the file explorer and open that picture in the %TEMPDIR%, I can see that the picture is rendered completely and it is not missing any portions, so the problem is not there - but it is probably in the way that PowerPoint adds this picture to the slide...

    Again, thanks for helping Tom - any feedback or idea you can give me on this matter is welcomed :)

    Tuesday, April 3, 2012 7:30 AM
  • HOLD IT! It appears that I was too quick to blame PowerPoint's .AddPicture() method - it seems that after all, the blame was indeed in my method which reads the file from the REST service.

    I have no idea why some of the pictures were displayed OK in the %TEMP% directory, but further inspection revealed that most of them weren't - which led me to revise my code using a different method to save the image from a Stream (I now use Image.FromStream() if anyone is interested to know) and now it displays just fine.

    Anyways, as for the other issue I had (with the 'MsoShapeType.msoPlaceholder' thing) - it appears now that the pictures are finally recognized as with the right type, but with one exception - when I add a new slide to the presentation, and then add a new picture to it (I have added a button on the ribbon that does that), the picture is added by default into the multi-object box which is generated automatically in every new slide (the one which says "Click to add text" and has 6 shortcuts icons in it to add a table, a picture, a chart etc.), and in that case - the picture which is added by my method is considered to be of 'MsoShapeType.msoPlaceholder' which kinda makes sense - so I'll "recycle" my initial question:

    Is there any way to prevent PowerPoint from adding the picture to that container by default? Maybe something in the way that I add the picture to a slide? Or should I just capture the event that is triggered whenever a new slide is created, and remove that aforementioned multi-object box programatically?




    Tuesday, April 3, 2012 9:02 AM
  • Hi Shay.livyatan,

    Would you please show me your snippet to create new slide?

    As far as I know you will insert different slide via modify your CustomLayout. I will show you a snippet which will add a empty slide (nothing in the slide).

            private void button2_Click(object sender, RibbonControlEventArgs e)
            {
                PowerPoint.Presentation p = Globals.ThisAddIn.Application.ActivePresentation;
                PowerPoint.CustomLayout layout = p.SlideMaster.CustomLayouts[7];
                p.Slides.AddSlide(1, layout);
    
            }

    I hope it can help you.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, April 4, 2012 7:24 AM
    Moderator
  • Hi Tom, and thanks again for replying.

    I'm not using any code to add slides - I'm leaving this entirely for the user to do.

    Anyways, I temporarily fixed this by programatically removing the 2nd Placeholder whenever a new slide (which is not the first slide) is created using the following code:

    void Application_PresentationNewSlide(Slide slide)
    {
        if (slide.SlideNumber > 1)
        {
            slide.Shapes.Placeholders[2].Delete();
        }
    }

    As I mentioned, I consider this to be a temporary fix, as I think that adding a new picture to a slide where it'd not automatically be embedded into that 2nd Placeholder - would be a much more elegant solution, but as deadlines approaches and the pressure levels gets higher - this solution will have to suffice for now ;)

    So - if you don't know of a way to add pictures the way I described above (e.g. NOT to be automatically added to the 2nd Placeholder), I'll just mark your first reply as the answer - you deserve it for all your trouble :)

    Wednesday, April 4, 2012 7:39 AM
  • Hi Shay.livyatan,

    As far as I know when you insert image from slide.Shapes.AddPicture will not insert image to Placeholder which has already existed. any images will have placeholder for themselves. I think might the exist placeholder effect your issue. So you work round seems only way to solve it.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Shay.livyatan Thursday, April 5, 2012 6:55 AM
    Thursday, April 5, 2012 4:46 AM
    Moderator