gpt4 book ai didi

C#:Powerpoint 不退出?

转载 作者:太空狗 更新时间:2023-10-30 01:11:51 24 4
gpt4 key购买 nike

我有一个脚本可以从我的应用程序打开 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);

}

}
}

最佳答案

如果其他人正在为此苦苦挣扎(在遍历幻灯片后无法关闭 PPT),即使在完成所有垃圾收集和释放资源之后,我今天大部分时间都在为这个问题挠头。我的解决方案是,我没有使用 foreach 遍历幻灯片,而是按如下方式进行:

        Microsoft.Office.Interop.PowerPoint.Application powerpoint;
Microsoft.Office.Interop.PowerPoint.Presentation presentation;
Microsoft.Office.Interop.PowerPoint.Presentations presentations;

powerpoint = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
powerpoint.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
presentations = powerpoint.Presentations;

presentation = presentations.Open(localPath, MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue);


//String presentationTitle = objPres.Name;
Microsoft.Office.Interop.PowerPoint.Slides slides = presentation.Slides;
**for (int i = 1; i <= slides.Count; i++)
{
Microsoft.Office.Interop.PowerPoint.Slide slide = slides[i];
String slideName = slide.Name;
releaseCOM(slide);
}**

这是我的 releaseCOM 方法:

    private static void releaseCOM(object o)
{
try
{
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(o);
}
catch { }
finally
{
o = null;
}
}

关于C#:Powerpoint 不退出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1624927/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com