Hi !
I have application which can export slides to PowerPoint. I'm using Microsoft.Office.Interop.PowerPoint.dll . This is the 11th version of assembly. I have ran application today and it seems that application throws an exception (Exception from HRESULT: 0x80070057 (E_INVALIDARG)). It only throws this exception when there is powerpoint application opened in system before i click export button. I'm adding a custom property to CustomDocumentProperties of Powerpoint presentation in order to identify which presentation was created by the application. This is the first time when I saw this exception. I have Windows 7 Ultimate x64 sytem with visual studio 2008 and 2010 and of course office 2007.
The part of code that causes the exception:
oDocAuthorProp = typeDocBuiltInProps.InvokeMember("Item",
BindingFlags.Default |
BindingFlags.GetProperty,
null, oDocBuiltInProps,
new object[] { strIndex });
What's wrong? I followed this article by Microsoft: http://support.microsoft.com/kb/303296
For over one year the Application has been working without any issue. What goes wrong? After 10 hours spent on searching the Internet for the answer I'm giving up.
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)
{
//ErrorHandling.LogErrorToFile(e.Message, e.StackTrace);
}
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: This kind of exception is being thrown only when there is opened at least one powerpoint window and presentation in that window hasn't been saved to disk yet. When there is opened at least one powerpoint windows with presentation already saved to disk, the application is working fine.