none
Wrong item access: subsequent reads gives same result RRS feed

  • Question

  •    Hello all,

     

       I am using interop for retrieving information about a Power Point presentation, and I'm having a really wrong behaviour when accessing different elements of a collection of properties.

       For example, imagine that I have a

    Microsoft.Office.Interop.PowerPoint.Shape s;
    
       that has a gradient fill with two different stops the first one at 0% and the second one at 70%.

     

       Using interop services, the next code

     

      float f1 = s.Fill.GradientStop[1].Position;
      float f2 = s.Fill.GradientStop[2].Position;
    

       should therefore store 0.0f in f1 and 0.7f in f2.

       Unfortunately, the final result is different: both variables store 0.0f.

       The really strange think is that if I switch the order in which I access the gradient stops collection:

     

      float f1 = s.....[2].Position;
      float f2 = s.....[1].Position;
    
    
       The final result is both variables having 0.7f.

       So, my conclusion is that the first access to the collection determines the results of every subsequent access .

       It is hard to imagine that this is an interop bug, so what am I doing wrong?

       Just in case someone may want to replicate the error, create a simple Power Point presentation with just one slide whose first shape (usually the title) has a fill gradient with two stops (at 0% and 70%) and save it to a file (c:\test.pptx).

       The program:

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using PowerPoint = Microsoft.Office.Interop.PowerPoint;
    using Microsoft.Office.Core;
    
    namespace Test
    {
      class Test
      {
        static void Test(string[] args)
        {
          var app = new PowerPoint.Application();
          PowerPoint.Presentation pp;
          if (app == null) return;
    
          try
          {
            pp = app.Presentations.Open("c:\\test.pptx", 
                          MsoTriState.msoFalse,
                          MsoTriState.msoFalse,
                          MsoTriState.msoTrue);
          }
          catch (Exception ex)
          {
            return;      
          }
    
          PowerPoint.Shape s = pp.Slides[1].Shapes[1];
    
          System.Console.WriteLine(s.Fill.GradientStops[1].Position);
          System.Console.WriteLine(s.Fill.GradientStops[2].Position);
    
          pp.Close();
    
          return;
        }
      }
    }
    
    

     

    instead of writing 0 and 0,7, will output

    0

    0

       But when swaping the WriteLine lines, the output becomes

    0,7

    0,7

     

       A final test reveals that this may be related to the COM communication, because when after performing the first access I release retrieve the COM objects again, the second access to the collection is done correctly:

     

          System.Console.WriteLine(s.Fill.GradientStops[1].Position);
          app = new PowerPoint.Application();
          pp = app.Presentations["test.pptx"];
          s = pp.Slides[1].Shapes[1];
          System.Console.WriteLine(s.Fill.GradientStops[2].Position);
    
    

     

       Output:

    0
    0,7
    
       The behaviour is still the same when using from and add-in, and I have observed something similar when accessing tab stops.

       Any ideas?

       Best regards!

    Friday, November 12, 2010 3:10 PM

Answers

  • Hi El Mogur

    I'd try asking this in a forum that specializes in PowerPoint. When you do, be sure to specify the version in which you're seeing this behavior.

    There is no PowerPoint forum on MSDN, only on "Answers", but that's where you'll find the object model specialists. They mostly know VB-speak, not C#, so I'd present the problem before any sample code. The specialists can probably tell you whether this is inherent in the object model for that version and if there's any workaround.

    http://social.answers.microsoft.com/Forums/en-US/group/Office

    Unfortunately, I can't move the message there for you. Instead, I'm going to move it to the general Office developer forum as your problem isn't related to the VSTO technology.

     


    Cindy Meister, VSTO/Word MVP
    Monday, November 15, 2010 5:23 PM
    Moderator