Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an application that posts database data to PowerPoint slide templates. One of the slides requires a picture as well. The following adds the picture to the slide:
C#
public void InsertImageInSlide()
{
    string SNMPicName = Session["SNMName"].ToString().Trim().Replace(" ", "_");
    Session["SNMPicName"] = SNMPicName;
    string strImgPath = "C:\\SNMPics\\" + SNMPicName + ".jpg";
    if (File.Exists(strImgPath))
    {
        Microsoft.Office.Interop.PowerPoint.Application ppApp;
        Microsoft.Office.Interop.PowerPoint.Presentation ppPrsn;
        Microsoft.Office.Interop.PowerPoint.Slide objSlide;
        Microsoft.Office.Interop.PowerPoint.Shape objShape;
        Microsoft.Office.Core.MsoTriState msoCFalse = Microsoft.Office.Core.MsoTriState.msoFalse;
        Microsoft.Office.Core.MsoTriState msoCTrue = Microsoft.Office.Core.MsoTriState.msoTrue;
        string strTemplateFile;
        ppApp = new Microsoft.Office.Interop.PowerPoint.Application();
        strTemplateFile = Session["pptxPowerpoint"].ToString().Trim();
        ppPrsn = ppApp.Presentations.Open(strTemplateFile);
        objSlide = ppApp.ActiveWindow.Presentation.Slides[1];
        objShape = ppPrsn.Slides[1].Shapes.AddPicture(strImgPath, msoCFalse, msoCTrue, 500, 150, 200, 250);
        ppPrsn.Save();
        ppPrsn.Close();
    }
}

In a VS2012 run without debugging, this code successfully adds the picture to the slide. However when the code is published to an IIS7 server, the code produces the following error when the application is invoked:
PowerPoint could not open the file. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Runtime.InteropServices.COMException: PowerPoint could not open the file.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 

[COMException (0x80004005): PowerPoint could not open the file.]
   Microsoft.Office.Interop.PowerPoint.Presentations.Open(String FileName, MsoTriState ReadOnly, MsoTriState Untitled, MsoTriState WithWindow) +0
   PowerPointTemplate.InsertImageInSlide() +631
   PowerPointTemplate.ParseTemplate() +1572
   GeneratePowerPoint.ProcessAllSlides() +534
   GeneratePowerPoint.BuildBriefSlides() +5159
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +155
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3804

The error occurs in the line containing AddPicture where PowerPoint cannot open the picture file. Any ideas?
Posted
Updated 19-Mar-15 11:27am
v2
Comments
Member 11428137 20-Mar-15 8:08am    
Does the account that ASP.NET is running under have permission to access the folder with the pictures?
Also looking at the stack trace, it looks like the error is actually at the line
ppPrsn = ppApp.Presentations.Open(strTemplateFile);
Have you checked that this is receiving a valid path?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900