Skip to content

Instantly share code, notes, and snippets.

@kbrammer
Last active December 16, 2015 07:29
Show Gist options
  • Save kbrammer/5399199 to your computer and use it in GitHub Desktop.
Save kbrammer/5399199 to your computer and use it in GitHub Desktop.
LINQPad script to migrate PowerPoint files from .ppt to .pptx using Microsoft.Office.Interop.Powerpoint
//using office 14.0.0.0
//using Microsoft.Office.Interop.Powerpoint
//http://msdn.microsoft.com/en-us/library/microsoft.office.interop.powerpoint._application(v=office.14).aspx
public static Microsoft.Office.Interop.PowerPoint.Application ppt { get; set; }
void Main()
{
ppt = new Microsoft.Office.Interop.PowerPoint.Application();
MigratePPT(@"C:\Users\MyFiles");
}
private static void MigratePPT(string path)
{
//Find all .ppt files recursively for given path
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(path);
FileInfo[] files = di.GetFiles("*.ppt", SearchOption.AllDirectories);
//NOTE: will overwrite files with same name if they have .pptx extension
foreach(FileInfo file in files)
{
if(file.Extension != ".pptx")
{
Console.WriteLine(string.Format("Converting {0}",file.FullName));
//open presentation
Presentation presentation = ppt.Presentations.Open(file.FullName, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
//add 'x' to end of filename
presentation.SaveAs(string.Format("{0}x",file.FullName), PpSaveAsFileType.ppSaveAsOpenXMLPresentation);
//close presentation
presentation.Close();
}
}
ppt.Quit();
Cleanup();
}
private static void Cleanup()
{
GC.Collect();
GC.WaitForPendingFinalizers();
// GC needs to be called twice in order to get the Finalizers called
// - the first time in, it simply makes a list of what is to be
// finalized, the second time in, it actually is finalizing. Only
// then will the object do its automatic ReleaseComObject.
GC.Collect();
GC.WaitForPendingFinalizers();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment