【发布时间】:2013-11-01 08:32:13
【问题描述】:
我有这个 C# 控制台应用程序尝试使用 Powerpoint 2013 将文件从 ppt 转换为 pdf。在冷启动时,它会因错误而崩溃:
System.Runtime.InteropServices.ComException:Error HRESULT E_FAIL has been returned from a call to a COM component.
at Microsoft.Office.Interop.PowerPoint.Presentations.Open(String FileName, MsoTriState ReadOnly, MsoTriState Untitled, MsoTriState WithWindow)
at ConsoleApplication4.Program.Main(String[] args)
--- End of inner exception stack trace ---
at ConsoleApplication4.Program.Main(String[] args)
如果我只是打开 Powerpoint 到一个新文件,它不会工作。
我必须手动打开一个包含信息的ppt,然后然后它会起作用。
在我的控制台程序运行之前,我可以做些什么来绕过手动打开 ppt 文件?我正在尝试将此实现为服务器上的自动过程,并且每次服务器重新启动时我都无法手动打开文件。
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
String sourceFilename = args[0];
String destinationFileName = args[1];
if (!File.Exists(sourceFilename))
{
throw new FileNotFoundException(string.Format("The specified file {0} does not exist.", sourceFilename), sourceFilename);
}
try
{
Microsoft.Office.Interop.PowerPoint.Application app = new Microsoft.Office.Interop.PowerPoint.Application();
app.Presentations.Open(sourceFilename,Microsoft.Office.Core.MsoTriState.msoFalse,Microsoft.Office.Core.MsoTriState.msoTrue,Microsoft.Office.Core.MsoTriState.msoFalse).SaveAs(destinationFileName, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPNG);
app.Quit();
}
catch (Exception e)
{
throw new Exception(string.Format("Unable to convert {0} to {1}", sourceFilename, destinationFileName), e);
}
}
}
}
我尝试添加这一行:
app.Presentations.Open(sourceFilename, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse).Close();
在SaveAs 行之前,尝试打开和关闭演示文稿,但在重新启动时,这并不能解决问题。
我还尝试添加:app.Activate() 和 app.Visible = MsoTriState.msoTrue,它会在崩溃前打开一个空白的 powerpoint 窗口。
【问题讨论】:
标签: c# ms-office powerpoint office-interop