none
How to access items in PowerPoint Guides collection? Type cast fails. RRS feed

  • Question

  • Hello everybody,

    I am developing an add-in for PowerPoint 2013 (v15.0) using C#, Visual Studio 2012 (v11.0) and VSTO targeting .net v4.0.

    Assume I have a presentation open with some guides shown. When trying to access its Guides collection items like this:

    Microsoft.Office.Interop.PowerPoint.Guides AllGuides = Globals.ThisAddIn.Application.ActivePresentation.Guides;
    Microsoft.Office.Interop.PowerPoint.Guide ThisGuide;
    if (AllGuides.Count > 0)
        ThisGuide = AllGuides[1];

    I get an InvalidCastException on the 4th line with this message:

    Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.PowerPoint.Guide'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{3D2F865B-E2DB-4896-BC35-6A006DF896DC}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

    Same message if ThisGuide variable is declared object instead. Sneaking around my way to the properties like this didn't help either, as I had feared:

    float Position = ((PowerPoint.Guide)AllGuides._Index(1)).Position;
    float Position = AllGuides[1].Position;

    Can somebody tell me what I'm doing wrong or how I can get around this?

    Thanks a lot!


    • Edited by h-ruddi Friday, July 4, 2014 9:26 AM Typo
    Friday, July 4, 2014 9:26 AM

Answers

  • Hello,

    I was able to reproduce the issue. This is certainly a bug in PowerPoint 2013 object model. Currently there doesn't seems to be a way to access guide either by using early binding or late binding. I have reported this behaviour to the Product group.

    • Proposed as answer by Ajay Bandooni Friday, July 11, 2014 5:34 PM
    • Marked as answer by h-ruddi Saturday, July 12, 2014 11:27 AM
    Friday, July 11, 2014 5:34 PM

All replies

  • Hello,

    You may find the Why cannot I cast my COM object to the interface it implements in C#? forum thread helpful. It describes a similar issues. Do you run another threads in the add-in?

    As a workaround you may call the required properties and methods using the late binding technology (reflection in .Net). See Type.InvokeMember method for more information. In that case you don't need to cast the object to the required type for accessing properties and methods.

    Friday, July 4, 2014 9:40 AM
  • Thanks, Eugene!

    My add-in does not start any additional threads, so I guess this is not the issue. As discussed in the thread you suggested, I added [STAThread] before the function and that does not seem to change anything.

    Their solution I suppose won't work for me as they solved it by registering the VB-generated TLB file with their C# project. No VB involved on my side and all other PPT type casts seem to work as expected, so I guess there is no library missed.

    Can you explain how you would proceed with InvokeMember? I tried a variety of different ways over the last 2hrs but did not get anywhere at all. Best guess so far seemed to be to get one item of AllGuides cast to object:

    Microsoft.Office.Interop.PowerPoint.Guides AllGuides = Globals.ThisAddIn.Application.ActivePresentation.Guides;
    IEnumerable<object> objEnum = AllGuides.Cast<object>();
    object SingleGuideAsObject = objEnum.ElementAt(1);
    

    But then SingleGuideAsObject still cannot be cast to PowerPoint.Guideand also these don't work:

    // This doesn't work - because my object is typeof(object) not PowerPoint.Guide? object obj = typeof(PowerPoint.Guide).InvokeMember("Position", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty, null, SingleGuideAsObject, null); // This doesn't work - COM Exception Name unknown object obj = typeof(object).InvokeMember("Position", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty, null, SingleGuideAsObject, null);

    I may be thinking completely wrong - any help appreciated!

    Friday, July 4, 2014 12:33 PM
  • // This doesn't work - because my object is typeof(object) not PowerPoint.Guide?
    object obj = typeof(PowerPoint.Guide).InvokeMember("Position", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty, null, SingleGuideAsObject, null);

    Do you get any error or exception in the code?

    The Type.InvokeMember page in MSDN provides sample code. Did you have a chance to look that page?

    Friday, July 4, 2014 2:08 PM
  • Yes, this line throws a TargetException with Message Object does not match target type.

    I did read through the msdn page and crafted my InvokeMember() calls after the examples shown there - obviously with some needed adjustments to the binding flags.

    Friday, July 4, 2014 2:46 PM
  • Hi,

    Thank you for posting in the MSDN Forum.

    I am trying to involve someone familiar with this topic to further look at this issue. There might be some time delay. Appreciate your patience.

    Sorry for any inconvenience and have a nice day!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, July 8, 2014 6:34 AM
    Moderator
  • Hello,

    I was able to reproduce the issue. This is certainly a bug in PowerPoint 2013 object model. Currently there doesn't seems to be a way to access guide either by using early binding or late binding. I have reported this behaviour to the Product group.

    • Proposed as answer by Ajay Bandooni Friday, July 11, 2014 5:34 PM
    • Marked as answer by h-ruddi Saturday, July 12, 2014 11:27 AM
    Friday, July 11, 2014 5:34 PM
  • Thanks, Ajay. Not what I hoped to hear, though. ;-) Thanks for forwarding.
    Saturday, July 12, 2014 11:27 AM