简体   繁体   English

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

[英]C# open PPTX file to a specific slide index

I am looking at opening a powerpoint file, .pptx, to a specific slide index.我正在考虑将 .pptx PowerPoint 文件打开到特定幻灯片索引。 For this example, lets say slide 3.对于这个例子,让我们说幻灯片 3。

I have code that opens the file, but I can not get it to open to Slide 3 on its own.我有打开文件的代码,但我无法让它自己打开到幻灯片 3。

Code to open pptx file:打开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());

I've done some reading, and my thoughts to solve this include adding a place where p.Slides[2] comes in to send it to slide index of 3 ( Thinking it goes 0, 1 ,2 like an array, I could be wrong here) .我已经做了一些阅读,我解决这个问题的想法包括添加一个地方p.Slides[2]进来将它发送到幻灯片索引 3(认为它像数组一样变成 0, 1 ,2,我可以这里错了)。 I just can not figure out where to put this in my code block, or if there is a different way, I have not seen that.我只是不知道将它放在我的代码块中的哪个位置,或者是否有不同的方式,我还没有看到。

As per Icemanind in the comments, adding in two lines after the p.Open line in the above code, the power point opened up to the correct slide.根据注释中的p.Open ,在上面代码中的p.Open行之后添加两行,电源点打开到正确的幻灯片。

Those two lines being:这两行是:

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

Functional Code:功能代码:

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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM