I have an application that can export slides to PowerPoint. I am using Microsoft.Office.Interop.PowerPoint.dll. This is the 11th version of the assembly. Today I launched the application, and it seems that the application throws an exception (Exception from HRESULT: 0x80070057 (E_INVALIDARG)). It throws this exception only when the powerpoint application opens on the system before clicking the export button. I am adding a custom property to the CustomDocumentProperties of a Powerpoint presentation to determine which presentation was created by the application. This is the first time I have seen this exception. I have a Windows 7 Ultimate x64 sytem with Visual Studio 2008 and 2010 and, of course, Office 2007.
The part of the code that throws the exception:
oDocAuthorProp = typeDocBuiltInProps.InvokeMember("Item",
BindingFlags.Default |
BindingFlags.GetProperty,
null, oDocBuiltInProps,
new object[] { strIndex });
What happened? I followed this Microsoft article: http://support.microsoft.com/kb/303296
For more than a year, the application has been working without any problems. Something went wrong? After 10 hours spent searching the Internet for an answer, I refuse.
here is the code of the Export2Powerpoint method:
public static void Export(List<ChartObject> chartObjects)
{
Application ppApp = new Application();
foreach (ChartObject chartObject in chartObjects)
{
chartObject.Chart.BorderLineStyle = ChartDashStyle.NotSet;
}
Presentation ppPress = null;
int i;
object oDocBuiltInProps;
string strValue = string.Empty;
if(ppApp.Presentations.Count == 0)
{
ppPress = AddPresentation(ppApp, ppPress);
}
else
{
i = 0;
object oDocAuthorProp = null;
foreach (Presentation presentation in ppApp.Presentations)
{
oDocBuiltInProps = presentation.CustomDocumentProperties;
Type typeDocBuiltInProps = oDocBuiltInProps.GetType();
string strIndex = "Browser";
try{
oDocAuthorProp = typeDocBuiltInProps.InvokeMember("Item",
BindingFlags.Default |
BindingFlags.GetProperty,
null, oDocBuiltInProps,
new object[] { strIndex }); ======>> here is the line that generates the exception
}
catch(Exception e)
{
}
if (oDocAuthorProp != null)
{
Type typeDocAuthorProp = oDocAuthorProp.GetType();
strValue = typeDocAuthorProp.InvokeMember("Value",
BindingFlags.Default |
BindingFlags.GetProperty,
null, oDocAuthorProp,
new object[] {}).ToString();
}
i++;
if(strValue == "true")
{
break;
}
}
if (strValue != string.Empty)
{
ppPress = ppApp.Presentations[i];
}
else
{
ppPress = AddPresentation(ppApp, ppPress);
}
}
i = ppPress.Slides.Count + 1;
int j = 0;
chartObjects.Reverse();
chartObjects.ForEach(chartObject =>
{
string directory = Path.Combine(System.Windows.Forms.Application.StartupPath, (i) + ".png");
PowerPoint.Slide actSlide = ppPress.Slides.Add(i, PpSlideLayout.ppLayoutBlank);
actSlide = ppPress.Slides[i];
Size size = chartObject.Chart.Size;
chartObject.Chart.ChartAreas[0].AxisX.LabelsAutoFitMinFontSize = 14;
chartObject.Chart.Size = new Size(RunTime.Configuration.ImageXSize,RunTime.Configuration.ImageYSize);
chartObject.Chart.SaveAsImage(directory, ChartImageFormat.Png);
chartObject.Chart.Size = size;
Image image = Image.FromFile(directory);
actSlide.Shapes.AddPicture(directory, MsoTriState.msoFalse,
MsoTriState.msoTrue, 0, 0, (int)ppPress.SlideMaster.Width, (int)ppPress.SlideMaster.Height);
image.Dispose();
File.Delete(directory);
j++;
});
chartObjects.Reverse();
ppApp.Visible = MsoTriState.msoTrue;
foreach (ChartObject chartObject in chartObjects)
{
chartObject.Chart.BorderLineStyle = ChartDashStyle.Solid;
}
}
EDIT: Such an exception is thrown only when at least one PowerPoint window is open and the view in this window has not yet been saved to disk. When at least one Powerpoint window is open with a saved record to disk, the application works fine.