san*_*der 3 c# powerpoint vsto
我正在开发一个创建PowerPoint演示文稿的C#程序.但是,我遇到以下指令的问题:
Presentation pres = pres_set.Open(path,
Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoTrue,
Microsoft.Office.Core.MsoTriState.msoTrue);
Run Code Online (Sandbox Code Playgroud)
该指令有时仅适用.如果没有,则会抛出"PowerPoint无法打开文件"消息的异常.然后,当我手动打开模板文件时,关闭它并再次执行该函数,大多数时候它将正确执行.
我使用过Microsoft Powerpoint 14.0和Microsoft Powerpoint 12.0库,但两者都有同样的问题.
有什么方法可以避免这个奇怪的问题吗?
答案要简单得多:Powerpoint应用程序需要现有文件.
我只是有相同的异常,因为我使用了相对路径,PowerPoint试图打开相对于PowerPoint.exe而不是Program.exe的文件.
在调用open-method之前,可以通过添加类似的东西来快速修复此问题:
// Start Powerpoint
var powerpointApp = new Microsoft.Office.Interop.PowerPoint.Application();
// Get a fileInfo object to generate the full path
FileInfo fileInfo = new FileInfo(@"RelativeDir\YourPresentation.pptx");
// Use the full path
powerpointApp.Presentations.Open(fileInfo.FullName, MsoTriState.msoTrue, WithWindow: MsoTriState.msoFalse);
Run Code Online (Sandbox Code Playgroud)