我有一個腳本,可以從我的應用程序打開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