Error when trying to open PowerPoint programmatica

2019-02-25 21:37发布

问题:

I have a method that uses Microsoft.Office.Interop.PowerPoint.Application interface to convert PowerPoint presentation slides into a jpeg files. In a service application that runs on Windows 2008 server it works without any issues.

Now however, I'm writing a new web application that uses the same process and the degubber chokes on the Presentation.Open call wiht an error: "PowerPoint could not open file".

My environment is Win 7, VS 2010. I'm using impersonation with a domain account that has admin privileges. The PowePoint file I'm trying to open is in the c:\temp folder which has NTFS rights of "everyone" set to full; file is NOT read-only, I can manually opent the file using a similar account as the domain account I'm using for impersonation. I don't know what the problem is. Can someone suggest what's going on?

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using AjaxControlToolkit;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;
using System.Threading;


public partial class _Default : System.Web.UI.Page
{
  private static int folderExtension;
  private static string tempSavePath,
                        fileName;

  protected void Page_Load(object sender, EventArgs e)
  {

  }

  private void Page_Error(object sender, EventArgs e)
  {
    Random r = new Random();
    int eventID = r.Next(1, 65535);
    Exception objErr = Server.GetLastError().GetBaseException();
    string err = "\nPage_Error Event" +
            "\n\nError in: " + Request.Url.ToString() +
            "\n\nError Message:" + objErr.Message.ToString() +
            "\n\nStack Trace:" + objErr.StackTrace.ToString() +
            "\n";
    uploadResult.Text = err.ToString();        
  }

  protected void AsyncFileUpload1_click(object sender, AsyncFileUploadEventArgs e)
  {
     // Temporary folder where the presentation file is uploaded.
     tempSavePath = @"c:\temp\";

     // Checks if there is a file present for uploading.
     if (AsyncFileUpload1.HasFile)
     {
        fileName = AsyncFileUpload1.FileName;

        try
        {
            AsyncFileUpload1.SaveAs(tempSavePath + fileName);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            ConvertToJpegs();
        }
     }
     else
     {
        uploadResult.Text = "No file to upload.";
     }
 }

 // Converts the presentation slides into individual jpeg files.
 protected void ConvertToJpegs()
 {
    Microsoft.Office.Interop.PowerPoint.Application app = new Microsoft.Office.Interop.PowerPoint.Application();
    Microsoft.Office.Core.MsoTriState ofalse = Microsoft.Office.Core.MsoTriState.msoFalse;
    Microsoft.Office.Core.MsoTriState otrue = Microsoft.Office.Core.MsoTriState.msoTrue;

    Presentation pptPresentation = null;

    try
    {
     **>>> It break here with the ".Open" call <<<**   
        pptPresentation = app.Presentations.Open(tempSavePath + fileName, ofalse, ofalse, ofalse);
        pptPresentation.SaveAs(tempSavePath + ".", PpSaveAsFileType.ppSaveAsJPG, ofalse);
        Thread.Sleep(500);
    }
    catch (Exception ex1)
    {
        throw ex1;
    }
    finally
    {
        pptPresentation.Close();
    }
  }
}

回答1:

First of all, what you are trying to achieve seems to be unsupported / not recommended by Microsoft:

All current versions of Microsoft Office were designed, tested, and configured to run as end-user products on a client workstation. They assume an interactive desktop and user profile. They do not provide the level of reentrancy or security that is necessary to meet the needs of server-side components that are designed to run unattended.

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution.

Source: http://support.microsoft.com/kb/257757

With that said, it seems that a couple of other people have experienced a similar behavior under these settings, so I suggest you take a look at:

  • Powerpoint could not open file
  • Powerpoint Interop fails in a Windows Service but works fine in a Windows Form application
  • Automating Office via Windows Service on Server 2008

And further, to check that you open the .pptx right, I just tried the PowerPoint Interop bits of your code sample in a C# .NET 4.5 console application using PowerPoint Interop v. 15 (for Office 2013) on a standard desktop PC. And that works out of the box.



回答2:

How do you run you web app? Is it IIS 7.5 or Asp.Ne development Server? If it's asp.net development server, you should start it as administrator. if that is IIS, i will need version of IIS.