J'ai une application qui peut exporter des diapositives vers PowerPoint. J'utilise Microsoft.Office.Interop.PowerPoint.dll. C'est la 11ème version de l'assemblage. J'ai couru l'application aujourd'hui et il semble que l'application jette une exception (Exception de HRESULT: 0x80070057 (E_INVALIDARG)). Il ne lance cette exception que lorsque l'application powerpoint est ouverte dans le système avant de cliquer sur le bouton d'exportation. J'ajoute une propriété personnalisée à la présentation CustomDocumentProperties de Powerpoint afin d'identifier quelle présentation a été créée par l'application. C'est la première fois que j'ai vu cette exception. J'ai Windows 7 Ultimate 64 bits sytem avec visual studio 2008 et 2010 et du bureau de cours 2007.Microsoft.Office.Interop.PowerPoint.dll PowerPoint CustomDocumentProperties gros problème
La partie du code qui provoque l'exception:
oDocAuthorProp = typeDocBuiltInProps.InvokeMember("Item",
BindingFlags.Default |
BindingFlags.GetProperty,
null, oDocBuiltInProps,
new object[] { strIndex });
Qu'est-ce qui ne va pas? J'ai suivi cet article par Microsoft: http://support.microsoft.com/kb/303296
Depuis plus d'un an, l'application fonctionne sans aucun problème. Qu'est-ce qui ne va pas? Après 10 heures passées à chercher sur Internet la réponse que j'abandonne.
est ici le code de la méthode Export2Powerpoint:
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: Ce genre d'exception est jetée seulement quand il est ouvert au moins une fenêtre powerpoint et présentation dans cette fenêtre n'a pas été enregistré sur le disque encore. Quand il y a au moins une fenêtre PowerPoint avec une présentation déjà enregistrée sur le disque, l'application fonctionne correctement.