我有一个脚本,可以从我的应用程序打开Powerpoint并导出所有幻灯片。之后,我需要关闭应用程序。C#:Powerpoint不退出?
我试过没有任何运气。能否请你帮忙?
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Powerpoint = Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;
using System.Runtime.InteropServices;
namespace PresentrBuilder
{
class PowerpointConverter
{
public static void Convert(String file, String safeFile)
{
Powerpoint.Application PP;
Powerpoint.Presentation Presentation;
PP = new Powerpoint.ApplicationClass();
PP.Visible = MsoTriState.msoTrue;
PP.WindowState = Microsoft.Office.Interop.PowerPoint.PpWindowState.ppWindowMinimized;
Presentation = PP.Presentations.Open(file, MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue);
// Voor elke slide, exporteren
String exportSlidesPath = Path.Combine(Properties.Settings.Default.CacheDir, @"presentatienaam1\slides");
// Kijk of de directory bestaat
if (!Directory.Exists(exportSlidesPath))
{
Directory.CreateDirectory(exportSlidesPath);
}
// Kijk of er al bestanden in de directory staan
// Zo ja: verwijderen
String[] files = Directory.GetFiles(exportSlidesPath, "*.png");
if (files.Length > 0)
{
foreach (string fileName in files)
{
File.Delete(Path.Combine(exportSlidesPath, fileName));
}
}
// Elke slide exporteren
foreach (Slide slide in Presentation.Slides)
{
slide.Export(Path.Combine(exportSlidesPath, "slide_" + slide.SlideIndex + ".png"), "PNG", 1024, 768);
Marshal.ReleaseComObject(slide);
}
GC.Collect();
GC.WaitForPendingFinalizers();
Marshal.ReleaseComObject(PP.Presentations);
Marshal.ReleaseComObject(Presentation.Slides);
Presentation.Close();
Marshal.FinalReleaseComObject(Presentation);
PP.Quit();
Marshal.FinalReleaseComObject(PP);
}
}
}
您已经编辑上面的代码,但它仍然是不正确的。您可以调用PP.Presentations(和Presentation.Slides)两次,一次使用它们,然后一次释放参考。然而,它不会像那样工作 - 每次调用它们时,都会创建另一个参考。您需要调用它们一次,然后将该参考存储在一个变量中,然后您可以释放它(根据Andre的答案)。 – 2009-10-26 15:46:33
[通过C#发起的PowerPoint不会退出]可能重复](http://stackoverflow.com/questions/981547/powerpoint-launched-via-c-sharp-does-not-quit) – 2012-08-07 18:17:52
尝试调用另一个收集并等待方法,在完成这个方法后,调用 – nawfal 2014-02-05 14:58:19