none
Saving Current Powerpoint as WMV RRS feed

  • Question

  • Hi,

    I have been asked to write a PowerPoint 2010 addin that converts the current presentation to a WMV file and FTPs it onto a remote server.

    But I am having some problems finding out when the "file save" process has completed so I can then start the FTP.

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

    The problem I have is that if I uncomment the code that checks if the file is locked the process that saves the file as a wmv never seems to start so it just keeps checking and checking going in a loop.

    If I leave the code exactly as above, with the file check not in there it works perfectly.

    Can anyone suggest how I can tell when the file has finished being written so I can trigger the FTP code please?

    Thanks

    Trev

    Friday, July 12, 2013 2:50 PM

Answers

  • Hi Daniel,

    The export progress will be started until the “btnLogin_Click” function being returned.

    In your code, the “while” statement blocked the “btnLogin_Click” function, so the export progress will be blocked and the whole progress will fall into an infinite loop.

    You can start a new thread to wait the exporting step finished.

    The sample code is below:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;
    using System.Threading;
    
    namespace PowerPointAddIn
    {
        public partial class TaskPaneControl : UserControl
        {
            public TaskPaneControl()
            {
                InitializeComponent();
    
                button1.Click += new EventHandler(button1_Click);
            }
    
            private void button1_Click(object obj, EventArgs args)
            {
                string exportName = "video_of_presentation";
                string exportPath = @"C:\{0}.wmv";
    
    
                var app = Globals.ThisAddIn.Application;
    
                app.Activate();
    
                app.ActivePresentation.SaveAs(
                    String.Format(exportPath, exportName), 
                    Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsWMV, Microsoft.Office.Core.MsoTriState.msoCTrue);
    
                var thread = new Thread((data) =>
                {
                    string path = data as string;
    
                    FileInfo file = new FileInfo(path);
    
                    while (IsFileLocked(file))
                    {
                        Thread.Sleep(500);
                    }
    
                    MessageBox.Show("Saved Video Successfully");
                });
    
                thread.Start(String.Format(exportPath, exportName));
            }
    
            protected virtual bool IsFileLocked(FileInfo file)
            {
                FileStream stream = null;
    
                try
                {
                    stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
                }
                catch (IOException)
                {
                    return true;
                }
                finally
                {
                    if (stream != null)
                        stream.Close();
                }
    
                return false;
            }
        }
    }
    

    Regards,

    Jeffrey

    Wednesday, July 17, 2013 2:00 AM
    Moderator

All replies

  • Hi ,

    Thank you for posting in the MSDN Forum.

    I'm trying to involve some senior engineers into this issue and it will take some time. Your patience will be greatly appreciated.

    Sorry for any inconvenience and have a nice day!

    Best regards,

    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, July 15, 2013 11:58 AM
    Moderator
  • Hi Daniel,

    The export progress will be started until the “btnLogin_Click” function being returned.

    In your code, the “while” statement blocked the “btnLogin_Click” function, so the export progress will be blocked and the whole progress will fall into an infinite loop.

    You can start a new thread to wait the exporting step finished.

    The sample code is below:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;
    using System.Threading;
    
    namespace PowerPointAddIn
    {
        public partial class TaskPaneControl : UserControl
        {
            public TaskPaneControl()
            {
                InitializeComponent();
    
                button1.Click += new EventHandler(button1_Click);
            }
    
            private void button1_Click(object obj, EventArgs args)
            {
                string exportName = "video_of_presentation";
                string exportPath = @"C:\{0}.wmv";
    
    
                var app = Globals.ThisAddIn.Application;
    
                app.Activate();
    
                app.ActivePresentation.SaveAs(
                    String.Format(exportPath, exportName), 
                    Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsWMV, Microsoft.Office.Core.MsoTriState.msoCTrue);
    
                var thread = new Thread((data) =>
                {
                    string path = data as string;
    
                    FileInfo file = new FileInfo(path);
    
                    while (IsFileLocked(file))
                    {
                        Thread.Sleep(500);
                    }
    
                    MessageBox.Show("Saved Video Successfully");
                });
    
                thread.Start(String.Format(exportPath, exportName));
            }
    
            protected virtual bool IsFileLocked(FileInfo file)
            {
                FileStream stream = null;
    
                try
                {
                    stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
                }
                catch (IOException)
                {
                    return true;
                }
                finally
                {
                    if (stream != null)
                        stream.Close();
                }
    
                return false;
            }
        }
    }
    

    Regards,

    Jeffrey

    Wednesday, July 17, 2013 2:00 AM
    Moderator
  • Hi Jeffrey,

    apologies for not returning to this thread and marking it answered. I had a few days holiday due.

    many thanks for your help.

    Trev

    Thursday, July 25, 2013 12:58 PM
  • Hi Jeffrey,

    apologies for not returning to this thread and marking it answered. I had a few days holiday due.

    many thanks for your help.

    Trev

    Thursday, July 25, 2013 12:58 PM