3

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.

1 Answer 1

1

I experienced what I suspect may be a similar problem (At the time, inexplicable exceptions on code that previously worked, but we did track it down) when embedded fonts were used by PowerPoint. It turned out to be an unanswered, invisible dialog box blocking the application, which the interop library turned into an exception. In essence, our experience was that there are many features of the PowerPoint application which aren't available to the interop classes, and if they're reached in normal operation then the interop code either hangs or breaks (usually breaks). So, in our case, there was no interop flag to allow the use of embedded fonts (in the PowerPoint application, when a file has embedded fonts, it forces a prompt (do you want to open this in readonly mode, or not at all?)) which must be answered, and because there's no ability in the interop libraries to specify your choice, the interop call throws an exception. So, my guess is that there's something going on here where the interop library cannot answer a "blocking" question posed by the PowerPoint application.

Of course, the most common reasons that we ran into this problem were all caused by Windows Update providing updates for Office which updates PowerPoint's security defaults. You might want to check that first.

If you were to perform that same action (change that field to "browser", as I think you appear to be doing) in a normal PowerPoint application window, on a presentation that hadn't been saved, would it present you with a dialog?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.