Click here to Skip to main content
16,015,351 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
C#
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
Updated 15-Oct-20 1:18am
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
[no name] 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
[no name] 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.

1 solution

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[^].

C#
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");
}
 
Share this answer
 
v6
Comments
[no name] 19-Dec-13 20:27pm    
realy an expert.
Member 12978737 15-Oct-20 5:12am    
Hi Mike,
We have done exactly as stated in the above post,Although this works on IIS Express i.e. Visual stdio, when we deploy on IIS server the video created does not embed the existing videos on the slides of the powerpoint. The Code created the final vide from power point without the videos which are present on individual slides of the presentation.
Any fixes for this ?
Mike Meinz 15-Oct-20 9:12am    
Wow! It has been almost seven years since I wrote that answer. Totally forgot about it.

See <a href="https://support.microsoft.com/en-us/office/turn-your-presentation-into-a-video-c140551f-cb37-4818-b5d4-3e30815c3e83">Turn your presentation into a video - PowerPoint</a>[<a href="https://support.microsoft.com/en-us/office/turn-your-presentation-into-a-video-c140551f-cb37-4818-b5d4-3e30815c3e83" target="_blank" title="New Window">^</a>] What parts of a presentation won't be included in a video? section.

If the information on that page does not help in your case, your can use video editing software to edit the newly saved PowerPoint video file inserting the videos used within your original PowerPoint file. I use CyberLink PowerDirector.
Mike Meinz 15-Oct-20 9:16am    
Sorry, I have no experience with IIS Express. When you find a solution, please add it here so others can benefit.

It could be related to the fact that when using Visual Studio, you are in a more permissive environment. Or it could be that the version of PowerPoint and whatever modules PowerPoint uses to create the video are different on the IIS server than on the IIS Express server. An older version of PowerPoint and/or supporting modules that does not play embedded videos may be on the IIS server.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900