检测 PowerPoint 何时完成保存文件 [英] Detect when PowerPoint has finished saving a file

查看:56
本文介绍了检测 PowerPoint 何时完成保存文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编写的加载项中保存 PowerPoint 文件时遇到问题.

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

基本上,我需要将当前打开的演示文稿另存为 wmv,然后通过 FTP 将其保存到外部服务器...听起来很简单吧?

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

我已经研究出如何将当前打开的演示文稿保存为 wmv.

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.

但是代码只是进入了一个无限循环.wmv 开始是为了写,但永远不会超过 0kb.

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

如果我删除该行

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;
    }

}

}

基于我发布的前一个线程,我还尝试了 Thread.Join() 以查看是否可以在继续之前简单地等待保存线程完成,但在保存文件时它根本没有暂停,所以我最终得到相同的结果.

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.

谢谢

推荐答案

PowerPoint 应用程序对象有一个 ppMediaTaskStatus 属性,它应该返回您需要的信息.您可以使用 PPT VBA IDE 中的对象浏览器获取各种值.

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 .

这篇关于检测 PowerPoint 何时完成保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆