I need to save powerpoints as HTML with c#. I also need to turn on animations with the 'ShowSlideAnimation' property. The first code block works fine, but when I duplicate the code with reflection, I am unable to get the 'WebOptions' property because it is null. I chose to do this without referencing the office assemblies because some of our clients may not have office installed.
Here is the code that works without reflection:
var ppt = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
var file = "C:\\Users\\username\\Desktop\\test.ppt";
var presentation = ppt.Presentations.Open(file,
Microsoft.Office.Core.MsoTriState.msoCTrue,
Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoFalse);
// ShowSlideAnimation set successfully
presentation.WebOptions.ShowSlideAnimation = Microsoft.Office.Core.MsoTriState.msoCTrue;
presentation.SaveAs("C:\\Users\\username\\Desktop\\test.html",
PpSaveAsFileType.ppSaveAsHTML,
Microsoft.Office.Core.MsoTriState.msoFalse);
presentation.Close();
Here is the code I am having problems with. I commented the lines that throw a NullReference exception. I cannot retrieve any PropertyInfo objects from the presentation object.
var coreAssembly = Assembly.Load("office, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c");
var pptAssembly = Assembly.Load("Microsoft.Office.Interop.PowerPoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c");
var ppt = pptAssembly.CreateInstance("Microsoft.Office.Interop.PowerPoint.ApplicationClass");
var presentations = ppt.GetType().GetProperty("Presentations").GetValue(ppt, null);
var msoTriStateType = coreAssembly.CreateInstance("Microsoft.Office.Core.MsoTriState").GetType();
var msoCTrue = msoTriStateType.GetField("msoCTrue").GetValue(msoTriStateType);
var msoFalse = msoTriStateType.GetField("msoFalse").GetValue(msoTriStateType);
string inputFile = "C:\\Users\\username\\Desktop\\test.ppt";
string outputFile = "C:\\Users\\username\\Desktop\\test.htm";
BindingFlags binding = BindingFlags.InvokeMethod | BindingFlags.Public;
object[] openArgs = new object[] { inputFile, msoCTrue, msoFalse, msoFalse };
object presentation = presentations.GetType().InvokeMember("Open", binding, null, presentations, openArgs);
// presentation object contains zero properties
//object webOptions = presentation.GetType().GetProperty("WebOptions").GetValue(presentation, null);
//webOptions.GetType().GetProperty("ShowSlideAnimation").SetValue(webOptions, msoCTrue, null);
var ppSaveAsFileType = pptAssembly.CreateInstance("Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType").GetType();
object ppSaveAsHTML = ppSaveAsFileType.GetField("ppSaveAsHTML").GetValue(ppSaveAsFileType);
presentation.GetType().InvokeMember("SaveAs", binding, null, presentation, new object[] { outputFile, ppSaveAsHTML, msoFalse });
presentation.GetType().InvokeMember("Close", binding, null, presentation, null);
ppt.GetType().InvokeMember("Quit", binding, null, ppt, null);