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

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

然而,现在,我正在编写一个新的Web应用程序,它使用相同的进程和在Presentation.Open调用上的degubber choke错误:“PowerPoint无法打开文件”。

我的环境是Win 7,VS 2010.我正在使用具有管理员权限的域帐户进行模拟。 我正在尝试打开的PowePoint文件位于c:\ temp文件夹中,该文件夹的NTFS权限为“everyone”设置为full; 文件不是只读的,我可以使用与我用于模拟的域帐户类似的帐户手动打开文件。 我不知道问题是什么。 有人可以说出正在发生的事吗? 谢谢,Risho。

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(); } } } 

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

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

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

如果要构建在服务器端上下文中运行的解决方案,则应尝试使用已安全执行无人参与的组件。 或者,您应该尝试找到允许至少部分代码在客户端运行的替代方法 。 如果从服务器端解决方案使用Office应用程序,则应用程序将缺少许多必要的function才能成功运行。 此外,您将承担整体解决方案稳定性的风险。

资料来源: http : //support.microsoft.com/kb/257757

话虽如此,似乎有其他几个人在这些设置下经历过类似的行为,所以我建议你看看:

  • Powerpoint无法打开文件
  • Powerpoint Interop在Windows服务中失败,但在Windows窗体应用程序中正常工作
  • 通过Server 2008上的Windows服务自动执行Office

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

你如何运行你的网络应用程序? 是IIS 7.5还是Asp.Ne开发服务器? 如果是asp.net开发服务器,则应以管理员身份启动它。 如果那是IIS,我将需要IIS的版本。