none
Cant save powerpoint after deleting links from excel using c# RRS feed

  • Question

  • I am using powerpoint and excel 2010. I have a c# program that opens powerpoint, updates the links from excel, then saves the file as a new filename. I then open the new filename and delete all the links. When I try to save the new filename, I get a COM exception.

    Here is the code. Can someone see what i am doing wrong?

    Thanks - TFox

    namespace A9

      public class UpdatePPLinks

    {

      public void DeleteLinks(PowerPoint.Presentation PRS)

    {

       foreach(Slide slide in PRs.Slides)

    {

        foreach(PowerPoint.Shape shape in slide.Shapes)

        {

           if(shape.Type.Equals(Microsoft.Office.Core.MsoShapeType.msoLinkedOLEObject))

           {

               shape.LinkFormat.BreakLink();

           } 

        }

    }

    public void init(string sourcefilename, string newfilename)

    {

      PowerPoint.Application ppt = new PowerPoint.Application();

      Powerpoint.Presentation PRS = ppt.Presentations.Open(sourcefilename,MsoTriState.False,MsoTriState.False,MsoTriState.True);

      PRS.UpdateLinks();

      PRS.SaveAs(newfilename);

      PRS.Close();

      ppt.Quit();

    // Next, we want to delete all the links in the powerpoint file and save it with now links

     

      PowerPoint.Application pp1t = new PowerPoint.Application();

      Powerpoint.Presentation new_PRS = ppt.Presentations.Open(newfilename,MsoTriState.False,MsoTriState.False,MsoTriState.True);

      DeleteLinks(new_PRS);

       new_PRS.Save(); // This is where I get the "Error HRESULT E_FAIL has been returned fro a call to a COM component

      new_PRS.Close();

      ppt1.Quit();

    }

    Tuesday, January 6, 2015 2:41 PM

Answers

  • Hi TFox,

    I think there're some errors in your code here:

    PowerPoint.Application ppt = new PowerPoint.Application();
    
    Powerpoint.Presentation PRS = ppt.Presentations.Open(sourcefilename,MsoTriState.False,MsoTriState.False,MsoTriState.True);
    
    PRS.UpdateLinks();
    
    PRS.SaveAs(newfilename);
    
    PRS.Close();
    
    ppt.Quit();
    
    // Next, we want to delete all the links in the powerpoint file and save it with now links
    
    
    
    PowerPoint.Application pp1t = new PowerPoint.Application();
    
    Powerpoint.Presentation new_PRS = ppt.Presentations.Open(newfilename,MsoTriState.False,MsoTriState.False,MsoTriState.True);
    
    DeleteLinks(new_PRS);
    new_PRS.Save(); // This is where I get the "Error HRESULT E_FAIL has been returned fro a call to a COM component 
    new_PRS.Close();
    
    ppt1.Quit();

    The underlined variable is actually "ppt1", right?

    When you call ppt.Quit() method, the ppt object will take some time to completely quite the process, but at this time you use ppt object to create another presentation, and it's unpredictable when the ppt object completely quite the process. If you call new_PRS.Save() method at this time, exception may be thrown out. So please make sure you use the correct variable and make sure the first Application has exited successfully before the second Application is created.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, February 2, 2015 9:36 AM
    Moderator

All replies

  • Did you try to call the SaveAs method instead? Does it work?
    Tuesday, January 6, 2015 3:47 PM
  • I tried SavaAs also and it does not work

    T

    Tuesday, January 6, 2015 4:59 PM
  • Hi TFox,

    I think there're some errors in your code here:

    PowerPoint.Application ppt = new PowerPoint.Application();
    
    Powerpoint.Presentation PRS = ppt.Presentations.Open(sourcefilename,MsoTriState.False,MsoTriState.False,MsoTriState.True);
    
    PRS.UpdateLinks();
    
    PRS.SaveAs(newfilename);
    
    PRS.Close();
    
    ppt.Quit();
    
    // Next, we want to delete all the links in the powerpoint file and save it with now links
    
    
    
    PowerPoint.Application pp1t = new PowerPoint.Application();
    
    Powerpoint.Presentation new_PRS = ppt.Presentations.Open(newfilename,MsoTriState.False,MsoTriState.False,MsoTriState.True);
    
    DeleteLinks(new_PRS);
    new_PRS.Save(); // This is where I get the "Error HRESULT E_FAIL has been returned fro a call to a COM component 
    new_PRS.Close();
    
    ppt1.Quit();

    The underlined variable is actually "ppt1", right?

    When you call ppt.Quit() method, the ppt object will take some time to completely quite the process, but at this time you use ppt object to create another presentation, and it's unpredictable when the ppt object completely quite the process. If you call new_PRS.Save() method at this time, exception may be thrown out. So please make sure you use the correct variable and make sure the first Application has exited successfully before the second Application is created.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, February 2, 2015 9:36 AM
    Moderator