C# 尝试以编程方式打开PowerPoint时出错

C# 尝试以编程方式打开PowerPoint时出错,c#,asp.net,office-interop,C#,Asp.net,Office Interop,我有一种使用Microsoft.Office.Interop.PowerPoint.Application界面将PowerPoint演示文稿幻灯片转换为jpeg文件的方法。在Windows 2008 server上运行的服务应用程序中,它可以正常工作 然而,现在我正在编写一个新的web应用程序,它使用相同的过程,而degubber在演示文稿中阻塞了。打开调用时出现错误:“PowerPoint无法打开文件” 我的环境是Win7,VS 2010。我正在对具有管理员权限的域帐户使用模拟。我试图打开的P

我有一种使用Microsoft.Office.Interop.PowerPoint.Application界面将PowerPoint演示文稿幻灯片转换为jpeg文件的方法。在Windows 2008 server上运行的服务应用程序中,它可以正常工作

然而,现在我正在编写一个新的web应用程序,它使用相同的过程,而degubber在演示文稿中阻塞了。打开调用时出现错误:“PowerPoint无法打开文件”

我的环境是Win7,VS 2010。我正在对具有管理员权限的域帐户使用模拟。我试图打开的PowePoint文件位于c:\temp文件夹中,该文件夹的NTFS权限“everyone”设置为full;文件不是只读的,我可以使用与我用于模拟的域帐户类似的帐户手动打开该文件。我不知道是什么问题。有人能告诉我发生了什么事吗

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();
    }
  }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.IO;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用AjaxControlToolkit;
使用Microsoft.Office.Core;
使用Microsoft.Office.Interop.PowerPoint;
使用系统线程;
公共部分类\u默认值:System.Web.UI.Page
{
私有静态int-folderExtension;
私有静态字符串tempSavePath,
文件名;
受保护的无效页面加载(对象发送方、事件参数e)
{
}
私有无效页面错误(对象发送方,事件参数e)
{
随机r=新随机();
int eventID=r.Next(165535);
异常objErr=Server.GetLastError().GetBaseException();
string err=“\n页面错误事件”+
“\n\n:”+Request.Url.ToString()中出现错误+
“\n\n错误消息:”+objErr.Message.ToString()+
“\n\n堆栈跟踪:”+objErr.StackTrace.ToString()+
“\n”;
uploadResult.Text=err.ToString();
}
受保护的void AsyncFileUpload1\u单击(对象发送方,AsyncFileUploadEventArgs e)
{
//用于上载演示文稿文件的临时文件夹。
tempSavePath=@“c:\temp\”;
//检查是否存在要上载的文件。
if(AsyncFileUpload1.HasFile)
{
fileName=AsyncFileUpload1.fileName;
尝试
{
AsyncFileUpload1.SaveAs(tempSavePath+fileName);
}
捕获(例外情况除外)
{
掷骰子;
}
最后
{
converttojpeg();
}
}
其他的
{
uploadResult.Text=“没有要上载的文件。”;
}
}
//将演示文稿幻灯片转换为单个jpeg文件。
受保护的void converttojpeg()
{
Microsoft.Office.Interop.PowerPoint.Application app=新的Microsoft.Office.Interop.PowerPoint.Application();
Microsoft.Office.Core.MsoTriState of ALSE=Microsoft.Office.Core.MsoTriState.msof ALSE;
Microsoft.Office.Core.MsoTriState otrue=Microsoft.Office.Core.MsoTriState.msoTrue;
演示文稿pptPresentation=null;
尝试
{

**>>>这里用“.Open”调用中断如何运行web应用程序?是IIS 7.5还是Asp.Ne开发服务器? 如果是asp.net开发服务器,则应以管理员身份启动它。
如果是IIS,我需要IIS的版本。

首先,Microsoft似乎不支持/不推荐您尝试实现的目标:

所有当前版本的Microsoft Office都经过了设计、测试和测试 配置为在客户端工作站上作为最终用户产品运行。它们 假设是交互式桌面和用户配置文件。它们不提供 满足以下条件所需的可重入性或安全性级别: 需要设计为无人值守运行的服务器端组件

Microsoft目前不推荐也不支持, 从无人值守的环境中实现Microsoft Office应用程序的自动化, 非交互式客户端应用程序或组件(包括ASP, NET、DCOM和NT服务),因为Office可能表现出不稳定 在此环境中运行Office时的行为和/或死锁

如果要构建在服务器端上下文中运行的解决方案,则 应尽量使用无人值守的安全组件 或者,您应该尝试找到至少允许 用于运行客户端的部分代码。如果使用Office应用程序 从服务器端解决方案来看,应用程序将缺少许多 成功运行所需的功能。此外,您将 以整体解决方案的稳定性冒险

资料来源:

话虽如此,似乎其他一些人在这些环境下也经历过类似的行为,所以我建议你看看:


此外,为了检查您是否正确打开了
.pptx
,我刚刚使用PowerPoint Interop v.15(适用于Office 2013)在C#NET 4.5控制台应用程序中尝试了代码示例中的PowerPoint Interop位在标准台式PC上。这是开箱即用的。

您应该使用注释来回答问题。当您有一些有用的内容可供操作时,应该使用答案。