Detect when PowerPoint has finished saving a file

半城伤御伤魂 提交于 2019-12-11 12:29:02

问题


I am having problems saving a PowerPoint file in an add-in I am writing.

Basically, I need to save the currently open presentation as a wmv and then FTP it to an external server... sounds easy eh?

I have worked out how to save the currently open presentation as a wmv.

I have also got code to check if a file is open so I can tell when the "save" process is complete.

But the code just goes into an infinite loop. The wmv starts for get written but never goes beyond 0kb.

If I remove the line

checkfile(exportPath, exportName);

it works just fine... otherwise it just stays in a loop.

This is the code I have so far...

using System;
using System.Windows.Forms;
using Office = Microsoft.Office.Core;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using System.IO;

namespace PowerPointAddIn2
{
public partial class LoginPanel : UserControl
{
    public LoginPanel()
    {
        InitializeComponent();
    }

    private void LoginPanel_Load(object sender, EventArgs e)
    {

    }

    private void btnLogin_Click(object sender, EventArgs e)
    {

        string exportName = "video_of_presentation";
        string exportPath = @"C:\{0}.wmv";

        // Export the currently open presentation
        PowerPoint.Application ppApplication = null;
        ppApplication = new PowerPoint.Application();
        ppApplication.Activate();
        ppApplication.ActivePresentation.SaveAs(String.Format(exportPath, exportName), PowerPoint.PpSaveAsFileType.ppSaveAsWMV, Office.MsoTriState.msoTrue);

        checkfile(exportPath, exportName);

        MessageBox.Show("Finished");

    }

    protected void checkfile(string exportPath, string exportName)
    {
        FileInfo f = new FileInfo(String.Format(exportPath, exportName));
        while (IsFileLocked(f) == true) { System.Threading.Thread.Sleep(5000); }
        MessageBox.Show("Finished");
    }

    protected virtual bool IsFileLocked(FileInfo file)
    {
        FileStream stream = null;

        try
        {
            stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
        }
        catch (IOException)
        {
            //the file is unavailable because it is:
            //still being written to
            //or being processed by another thread
            //or does not exist (has already been processed)
            return true;
        }
        finally
        {
            if (stream != null)
                stream.Close();
        }

        //file is not locked
        return false;
    }

}

}

Based on a previous thread I posted I also tried Thread.Join() to see if I could simply wait for the save thread to finish before I continued but it didn't pause at all while the file was being saved so I ended up with the same result.

Any help/pointers would be very much appreciated.

Thanks


回答1:


The PowerPoint Application object has a ppMediaTaskStatus property that should return the info you need for this. You can get the various values using the Object Browser in the PPT VBA IDE .



来源:https://stackoverflow.com/questions/17618620/detect-when-powerpoint-has-finished-saving-a-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!