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