0
votes

I am developing a VSTO application for PowerPoint using C#. The goal is to export the selected slide of the opened PowerPoint presentation to PNG file on user's computer every 5 seconds. PowerPoint API provides the following way to export the slide:

(Slide)Application.ActiveWindow.View.Slide.Export("D:/path", "png")

However, every time this method is called, PowerPoint window freezes (maybe deactivates?) for a split second, and because of this any expanded menus get closed (for example, the menu opened by right-clicking on a slide, menu for inserting shapes, etc.)

I am looking for a way to avoid this. Is there a way to fix this issue when using Slide.Export method? Or maybe there are some alternatives to using it?

I tried using custom libraries like Aspose.Slides, and they can fix this issue, but cause an even worse one: they can't access the Presentation object presented by PowerPoint assembly, so in order to use them on your assembly, you would have to save a copy to the computer and open it, which is a bad solution in my case.

Any ideas on how to fix my issue will be very helpful.

Edit: to reproduce the issue, create a VSTO add-in project for PowerPoint and replace ThisAddIn with the following code:

public partial class ThisAddIn
{
    public Form form = new Form
    {
        Opacity = 0.01,
        Visible = false,
    };
    delegate void InvokeEventHandler();

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        form.Show();

        var del = new InvokeEventHandler(() => Timer_Tick());
        form.Invoke(del);
        var timer = new System.Timers.Timer();
        timer.Interval = 5000;
        timer.Elapsed += (s, ea) => form.Invoke(del);
        timer.Start();
    }

    private void Timer_Tick()
    {
        try
        {
            var slide = (Slide)Application.ActiveWindow.View.Slide;
            slide.Export(Path.Combine(Path.GetTempPath(), "test"), "png");
        }
        catch
        {
            return;
        }
    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
    }

    #region VSTO generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InternalStartup()
    {
        this.Startup += new System.EventHandler(ThisAddIn_Startup);
        this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
    }
    
    #endregion
}

When PowerPoint opens, right-click on a slide and wait for a couple of seconds. When timer ticks, the menu will be closed.

1
Can you post your timer code? - EylM
@EylM I added ThisAddIn code in the bottom - Kirill Prikhodko
My bet here is that you are accessing the Powerpoint objects from a different thread (System.Timers.Timer). You should use the main thread via Invoke. - EylM
I tried to do this, but the result is the same (see updated code above). I might be doing something wrong though, can you please check? - Kirill Prikhodko

1 Answers

0
votes

The specified graphics format must have an export filter registered in the Windows registry. You can specify either the registered extension or the registered filter name. Microsoft PowerPoint will first search for a matching extension in the registry. If no extension that matches the specified string is found, PowerPoint will look for a filter name that matches.

Try using the JPG file format instead:

With Application.ActivePresentation.Slides(3)
    .Export "c:\my documents\Graphic Format\" & _
        "Slide 3 of Annual Sales", "JPG"
End With