You need to wait for the video to be completed. Your code exits PowerPoint before the video has been completed. Below is an example that works.
Notes
* I changed your variable
path
to
strPath
so that I could use
Path.Combine
.
* I changed
PowerPoint._Presentation
to
PowerPoint.Presentation
.
Additional
There are other values of
PpMediaTaskStatus
that you should check after the
while
loop. See
PpMediaTaskStatus Enumeration[
^].
if (!string.IsNullOrEmpty(strPath)) {
bool validName = false;
string nFile = "newFile";
PowerPoint.Application objApp;
PowerPoint.Presentation objPres;
objApp = new PowerPoint.Application();
objApp.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
objApp.WindowState = PowerPoint.PpWindowState.ppWindowMinimized;
objPres = objApp.Presentations.Open(txtSearch.Text, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoTrue);
Console.WriteLine("File name: " + txtSearch.Text);
try {
txtStat.Text = "Starting Conversion";
if (!nFile.Contains(".wmv")) {
nFile += ".wmv";
}
objPres.SaveAs(System.IO.Path.Combine(strPath, nFile), PowerPoint.PpSaveAsFileType.ppSaveAsWMV, MsoTriState.msoTrue);
while (objApp.ActivePresentation.CreateVideoStatus == PowerPoint.PpMediaTaskStatus.ppMediaTaskStatusInProgress || objApp.ActivePresentation.CreateVideoStatus == PowerPoint.PpMediaTaskStatus.ppMediaTaskStatusQueued) {
Application.DoEvents();
System.Threading.Thread.Sleep(500);
}
txtStat.Text = "Done";
objPres.Close();
objApp.Quit();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(objPres);
objPres = null;
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(objApp);
objApp = null;
GC.Collect();
GC.WaitForPendingFinalizers();
} catch (Exception ex) {
Console.WriteLine(ex);
}
} else {
MessageBox.Show("Please select a file PowerPoint file to convert");
}