简体   繁体   中英

Open PowerPoint presentation in slideshow mode - C#

I'm trying to open a PowerPoint presentation directly in slide show mode. I've tried using a Process (as shown below), but I get an error message from PowerPoint saying it can't find the file, error message "PowerPoint can't read C://Users/Route%20Plotter.pptx". The issue is caused by the white space in the file name as it works when this is removed.

string powerPointPath = @"C:\Program Files\Microsoft Office   15\root\office15\powerpnt.exe";
string powerPointFilePath = "\"" + "C://Users/Route Plotter.pptx" + "\"";

Process powerPoint = new Process();
powerPoint.StartInfo.FileName = powerPointPath;
powerPoint.StartInfo.Arguments = " /S " + powerPointFilePath;
powerPoint.Start();

I've tried using the Office introp method (below), but can't get that to open directly in slideshow mode.

Microsoft.Office.Interop.PowerPoint.Application pptApp = new Microsoft.Office.Interop.PowerPoint.Application();
pptApp.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
pptApp.Activate();

Microsoft.Office.Interop.PowerPoint.Presentations ps = pptApp.Presentations;
Microsoft.Office.Interop.PowerPoint.Presentation p = ps.Open(powerPointFilePath, 
            Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue)

Any ideas how to stop it changing the whitespace to a %20 (I'm already adding quotes round the path), or another way to open the file directly into slideshow mode, would be appreciated.

(I'm using VS2013 and PowerPoint 2013).

Thanks to DavidG, the issue was the direction of the slashes. Forward slashes ( / ) are for URI and back slashes ( \\ ) are for file paths. Replacing the forward slashes with backslashes fixes the issue.

Following code will be used to run the slide show mode for Power Point. Just you replace the file path is enough.

        Application ppApp = new Application();
        ppApp.Visible = MsoTriState.msoTrue;

        Presentations ppPresens = ppApp.Presentations;
        Presentation objPres = ppPresens.Open("C:\\Users\\Users\\Documents\\Projects\\LS\\WindowsFormsApp1\\PPT.pptx", MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue);
        Slides objSlides = objPres.Slides;

        SlideShowWindows objSSWs;
        SlideShowSettings objSSS;
        //Run the Slide show                                
        objSSS = objPres.SlideShowSettings;
        objSSS.Run();
        objSSWs = ppApp.SlideShowWindows;
        while (objSSWs.Count >= 1) System.Threading.Thread.Sleep(100);
        //Close the presentation without saving changes and quit PowerPoint                             
        objPres.Close();
        ppApp.Quit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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