Click here to Skip to main content
14,297,637 members
Rate this:
Please Sign up or sign in to vote.
See more:
if (path != "")
{
    bool validName = false;
    string nFile = "newFile";

    PowerPoint._Presentation objPres;

    objApp = new PowerPoint.Application();
    objApp.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
    objApp.WindowState = Microsoft.Office.Interop.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(path + "\\" + nFile, PowerPoint.PpSaveAsFileType.ppSaveAsWMV, MsoTriState.msoTrue);


        txtStat.Text = "Done";
        objPres.Close();
        objApp.Quit();
        
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
}
else
{
    MessageBox.Show("Please select a file PowerPoint file to convert");
}


Above is my I am using to try and convert a PowerPoint into a video. I am using Microsoft.Office.Interop.PowerPoint in order to do this. My issue is that it converts the first slide but after that it displays really strange looking green bars and weird colors.

Any help with this will be greatly appreciated!
Posted
Comments
Mike Meinz 19-Dec-13 14:29pm
   
If you use File / Save As in PowerPoint without automation (i.e. via the keyboard), does it convert correctly then?

Also, see How to Release COM Interop Objects so the Called Program Can Exit
bferk 19-Dec-13 14:37pm
   
When we try it in PowerPoint it works, but for some reason the code above does not. Okay thanks for the tip, I will check that article out.
Mike Meinz 20-Dec-13 9:43am
   
I posted Solution 1 which solves the problem.
Sergey Alexandrovich Kryukov 19-Dec-13 15:38pm
   
Why? Office PowerPoint does have an option to save it into a video.
—SA
bferk 19-Dec-13 15:41pm
   
The reason I want to do this in code is because I will be getting large amounts of PowerPoints sent to me that need to be converted, so I want to do this to make it easier on me.
Sergey Alexandrovich Kryukov 19-Dec-13 23:06pm
   
Understood.
—SA

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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);
		// Wait for creation of video file
		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();
                // Release COM Objects
		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");
}
   
v6
Comments
idle63 19-Dec-13 20:27pm
   
realy an expert.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100