How can I embed a PowerPoint presentation into a WPF application without opening another window?

Brandon Dusseau picture Brandon Dusseau · Mar 31, 2013 · Viewed 10.3k times · Source

Currently I have a WPF application in C#, but I'm finding it to be incredibly difficult to find any useful ways to embed a PowerPoint presentation into my window.

One solution I found here: Embedding a Powerpoint show into a C# application

This solution created the problem of having PowerPoint run in another window, but just display its UI within the WPF application. This meant that when the WPF window was focused, the PowerPoint presentation was not, and stopped playing. There was also the problem of PowerPoint crashing when the window was closed.

Another solution I found was here: http://www.codeproject.com/Articles/118676/Embedding-PowerPoint-presentation-player-into-a-WP

The solution was popular, but I found it difficult to work with. I don't know any Win32 programming, OR C++, so I found it extremely hard to modify. I managed to get it to stop displaying a second copy of the PowerPoint (an intended function in the original project), but I've not yet found a way to automatically open the PowerPoint presentation.

So what I need is a way to cleanly open the PowerPoint presentation automatically and in the background (I don't want the PowerPoint UI to be displayed at any point), and to allow it to run automatically (and not respond to input) while the application is running. It would be wonderful if I could keep it within C# and WPF, and not have to deal with Win32 and C++.

Is this possible? At this point I'm really regretting this project simply because of the PowerPoint integration headaches.

Answer

Joulukuusi picture Joulukuusi · Apr 4, 2013

You could convert your presentation to a video format on-the-fly:

// not tested as I don't have the Office 2010, but should work
private string GetVideoFromPpt(string filename)
{
    var app = new PowerPoint.Application();
    var presentation = app.Presentations.Open(filename, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);

    var wmvfile = Guid.NewGuid().ToString() + ".wmv";
    var fullpath = Path.GetTempPath() + filename;

    try
    {
        presentation.CreateVideo(wmvfile);
        presentation.SaveCopyAs(fullpath, PowerPoint.PpSaveAsFileType.ppSaveAsWMV, MsoTriState.msoCTrue);
    }
    catch (COMException ex)
    {
        wmvfile = null;
    }
    finally
    {
        app.Quit();
    }

    return wmvfile;
}

And then you would play it with MediaElement:

<MediaElement Name="player" LoadedBehavior="Manual" UnloadedBehavior="Stop" />

public void PlayPresentation(string filename)
{
    var wmvfile = GetVideoFromPpt(filename);
    player.Source = new Uri(wmvfile);
    player.Play();
}

Don't forget to File.Delete(wmvfile) when you're done playing the video!