JohK
JohK

Reputation: 19

Create a PPT using C# without having to kill a running powerpnt process

I've been trying to create a PPT with C# using something like the code below.

Microsoft.Office.Interop.PowerPoint.Application pptApp = new Microsoft.Office.Interop.PowerPoint.Application();

The problem is, if there's another powerpnt process running, the line of code above doesn't do anything (even when debugging it). The application just hangs infinitely.

Is there a way to be able to generate PPTs without having to kill an existing powerpnt process?

Upvotes: 1

Views: 1358

Answers (2)

Andrey Potapov
Andrey Potapov

Reputation: 67

With Aspose.Slides for .NET, you don't need to install PowerPoint to generate presentations with a single line of code:

using (Presentation presentation = new Presentation())
{
    // Add content to the presentation here
    // ...
    presentation.Save("output.pptx", SaveFormat.Pptx); // You can choose many other formats
}

Also, you can see your results by using Online Viewer here.

I work at Aspose.

Upvotes: 2

lidqy
lidqy

Reputation: 2453

In NET Framework you can hook into the "Running Object Table" (ROT) and get a running Powerpoint instance using InteropServices.Marshal. Then you can work with the running instance. If it is not running you can start it.

        using System.Runtime.InteropServices;
        using PowerPointApp = Microsoft.Office.Interop.PowerPoint.Application;

        PowerPointApp ppApp = null;
        try
        {
            var ppType = System.Type.GetTypeFromProgID("PowerPoint.Application");
            var ppRaw  = Marshal.GetActiveObject("PowerPoint.Application");
            ppApp  = ppRaw as PowerPointApp;
        }
        catch
        {
        }
        if (ppApp == null)
        {

            ppApp = new PowerPointApp();
        }

Marshal.GetActiveObject is not available in .NET Core and current .NET 5/6

Upvotes: 0

Related Questions