C#打开PPTX文件到特定幻灯片索引

C#打开PPTX文件到特定幻灯片索引,c#,C#,我想打开一个powerpoint文件.pptx到一个特定的幻灯片索引。对于本例,让我们假设幻灯片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文件.pptx到一个特定的幻灯片索引。对于本例,让我们假设幻灯片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,我可能错了)。我只是不知道该把它放在我的代码块中的什么地方,或者如果有不同的方法,我还没有看到。

根据评论中的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());

您应该能够做到这一点:
p.SlideShowWindow.View.GotoSlide(3,MsoTriState.msoFalse)
@Icemanind我在
p=ps.open
行之后添加了那一行,我的演示文稿(未知成员):请求无效。此演示文稿当前没有幻灯片放映视图。请先使用以下命令运行幻灯片放映:
p.SlideShowSettings.Run()
。然后,在上面加上我贴的那句话。成功!谢谢大家!@冰岛