首页 > 解决方案 > C#打开PPTX文件到特定幻灯片索引

问题描述

我正在查看打开特定幻灯片索引的 .pptx PowerPoint 文件。对于此示例,假设幻灯片 3。

我有打开文件的代码,但我不能让它自己打开到幻灯片 3。

打开pptx文件的代码:

string file_path = @"C:\Users\Me\Desktop\Test_Folder\myppt.pptx";
Microsoft.Office.Interop.PowerPoint.Application pptApp = new Microsoft.Office.Interop.PowerPoint.Application();
Microsoft.Office.Core.MsoTriState ofalse = Microsoft.Office.Core.MsoTriState.msoFalse;
Microsoft.Office.Core.MsoTriState otrue = Microsoft.Office.Core.MsoTriState.msoTrue;
pptApp.Visible = otrue;
pptApp.Activate();
Microsoft.Office.Interop.PowerPoint.Presentations ps = pptApp.Presentations;
Microsoft.Office.Interop.PowerPoint.Presentation p = ps.Open(file_path, ofalse, ofalse, otrue);
System.Diagnostics.Debug.Print(p.Windows.Count.ToString());

我已经阅读了一些内容,我解决这个问题的想法包括添加一个位置,p.Slides[2]将其发送到幻灯片索引 3(认为它像数组一样变为 0、1、2,我在这里可能是错的)。我只是不知道将它放在我的代码块中的哪个位置,或者如果有不同的方式,我还没有看到。

标签: c#

解决方案


根据评论中的 Icemanind,在p.Open上述代码行之后添加两行,电源点打开到正确的幻灯片。

这两行是:

p.SlideShowSettings.Run()
p.SlideShowWindow.View.GotoSlide(3, MsoTriState.msoFalse);

功能代码:

string file_path = @"C:\Users\Me\Desktop\Test_Folder\myppt.pptx";
Microsoft.Office.Interop.PowerPoint.Application pptApp = new Microsoft.Office.Interop.PowerPoint.Application();
Microsoft.Office.Core.MsoTriState ofalse = Microsoft.Office.Core.MsoTriState.msoFalse;
Microsoft.Office.Core.MsoTriState otrue = Microsoft.Office.Core.MsoTriState.msoTrue;
pptApp.Visible = otrue;
pptApp.Activate();
Microsoft.Office.Interop.PowerPoint.Presentations ps = pptApp.Presentations;
Microsoft.Office.Interop.PowerPoint.Presentation p = ps.Open(file_path, ofalse, ofalse, otrue);
p.SlideShowSettings.Run()
p.SlideShowWindow.View.GotoSlide(3, MsoTriState.msoFalse);
System.Diagnostics.Debug.Print(p.Windows.Count.ToString());

推荐阅读