none
C# “PowerPoint found unreadable content” and “Error HRESULT E_FAIL has been returned from a call to a COM component” RRS feed

  • Question

  • Hi, so right now I am trying to mainly go off of this http://blogs.msdn.com/b/brian_jones/archive/2008/11/18/creating-a-presentation-report-based-on-data.aspx to try and copy slides and replace the text in them. However I have run in to a few problems, for one when I try to run the PowerPoint within my C# program with Microsoft.Office.Interop.PowerPoint I get the error “Error HRESULT E_FAIL has been returned from a call to a COM component”.  And then when I try to just open the PowerPoint to see what it looks like I get the error “PowerPoint found unreadable content” and then after clicking ok for the error the slide will open up, but the first slide is blank. However after that it will have the copied slides with the changes I made to the text, so I got it to do what  I mainly wanted to do, but the 1 image on the slide will be missing and have a box with a red X with the error” The image cannot be displayed”.  I have tried to take the image out of the PowerPoint just to see if the errors  would go away, but they don’t.

     

    So does anyone know how to fix this?

     

    Here is the code I have so far just trying to get it to work.

     

    Thanks

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Microsoft.Office.Core;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Presentation;
    using Drawing = DocumentFormat.OpenXml.Drawing;
    using PowerPoint = Microsoft.Office.Interop.PowerPoint;
    using DocumentFormat.OpenXml;
    using System.IO;
    using System.Data;
    
    namespace v2CreateSlidesFromDB
    {
        class Program
        {
            int i = 1;
    
            static void Main(string[] args)
            {
                Program This = new Program();
                This.GenerateSlidesFromDB();
            }
    
            
    
            private void runShow()
            {
    
                String strTemplate;
                strTemplate = "C:\\Users\\ncsd\\Documents\\Visual Studio 2010\\Projects\\mainPowerPointController\\mainPowerPointController\\bin\\Debug\\output.pptx";
    
                PowerPoint.SlideShowSettings objSSS;
                PowerPoint._Presentation objPres;
                PowerPoint.Presentations objPresSet;
                PowerPoint.Application objApp;
    
                objApp = new PowerPoint.Application();
                objApp.Visible = MsoTriState.msoTrue;
                objPresSet = objApp.Presentations;
    
                objPres = objPresSet.Open(strTemplate,
                    MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue);
    
                objSSS = objPres.SlideShowSettings;
                objSSS.Run();
    
            }
    
    
            void SwapPlaceholderText(SlidePart slidePart, string placeholder, string value)
            {
                List<Drawing.Text> textList = slidePart.Slide.Descendants<Drawing.Text>().Where(
                   t => t.Text.Equals(placeholder)).ToList();
    
                foreach (Drawing.Text text in textList)
                {
                    text.Text = value;
                }
               
            }
    
    
            void InsertContent(SlidePart slidePart, string type, string zone)
            {
                SwapPlaceholderText(slidePart, "TYPE", type);
                SwapPlaceholderText(slidePart, "ZONE", zone);
            }
    
            void SwapPhoto(SlidePart slidePart, string imgId)
            {
    
                Drawing.Blip blip = slidePart.Slide.Descendants<Drawing.Blip>().First();
    
                blip.Embed = imgId;
    
                slidePart.Slide.Save();
    
            }
    
            SlidePart CloneSlidePart(PresentationPart presentationPart, SlidePart slideTemplate)
            {
                SlidePart newSlidePart = presentationPart.AddNewPart<SlidePart>("newSlide" + i);
                i++;
                newSlidePart.FeedData(slideTemplate.GetStream(FileMode.Open));
    
                newSlidePart.AddPart(slideTemplate.SlideLayoutPart);
    
                SlideIdList slideIdList = presentationPart.Presentation.SlideIdList;
    
                uint maxSlideId = 1;
                SlideId prevSlideId = null;
                foreach (SlideId slideId in slideIdList.ChildElements)
                {
                    if (slideId.Id > maxSlideId)
                    {
                        maxSlideId = slideId.Id;
                        prevSlideId = slideId;
                    }
                }
    
                maxSlideId++;
    
                SlideId newSlideId = slideIdList.InsertAfter(new SlideId(), prevSlideId);
                newSlideId.Id = maxSlideId;
                newSlideId.RelationshipId = presentationPart.GetIdOfPart(newSlidePart);
    
                return newSlidePart;
            }
    
            void DeleteTemplateSlide(PresentationPart presentationPart, SlidePart slideTemplate)
            {
                SlideIdList slideIdList = presentationPart.Presentation.SlideIdList;
    
                foreach (SlideId slideId in slideIdList.ChildElements)
                {
                    if (slideId.RelationshipId.Value.Equals("rId3"))
                    {
                        slideIdList.RemoveChild(slideId);
                    }
                }
    
                presentationPart.DeletePart(slideTemplate);
            }
    
            void GenerateSlidesFromDB()
            {
                string slideName = @"output.pptx";
    
                int i = 0;
    
                
              
    
                File.Copy(@"C:\Brett's notes\Presentation1.pptx", slideName, true);
    
                using (PresentationDocument presentationDocument = PresentationDocument.Open(slideName, true))
                {
                    PresentationPart presentationPart = presentationDocument.PresentationPart;
    
                    SlidePart slideTemplate = (SlidePart)presentationPart.GetPartById("rId2");
    
                    while (i < 5)
                    {
                        string zone = "Zone1";
                        string type = "Crime Incidents";
    
    
                        SlidePart newSlide = CloneSlidePart(presentationPart, slideTemplate);
    
    
                        InsertContent(newSlide, zone, type);
                        newSlide.Slide.Save();
                  
                        i++;
                    }
    
                    
    
                    DeleteTemplateSlide(presentationPart, slideTemplate);
    
                    presentationPart.Presentation.Save();
    
    
                    runShow();
    
                }
    
            }
        }
    }
    
    

    Monday, February 6, 2012 5:54 PM

Answers

  • Hi Ghoroo,

    Thanks for posting in the MSDN Forum.

    I think the exception which your shown in the tilte occurred because OpenXml SDK didn't release the Presentation from the memory. It works fine if you move runShow() method out of the "using" statement around.

    Have a good day,

    Tom


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

    Wednesday, February 8, 2012 10:21 AM
    Moderator

All replies

  • Or if anyone knows how I could do the same thing with Interop instead that would also work, I tried looking, but I couldn't find a good example of how you do it with Interop.

    Monday, February 6, 2012 6:51 PM
  • Hi Ghoroo,

    Thanks for posting in the MSDN Forum.

    I think the exception which your shown in the tilte occurred because OpenXml SDK didn't release the Presentation from the memory. It works fine if you move runShow() method out of the "using" statement around.

    Have a good day,

    Tom


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

    Wednesday, February 8, 2012 10:21 AM
    Moderator