How do I Reference an Item in a Collection by Name

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32656

    How do I Reference an Item in a Collection by Name

    This may be more complicated than indicated by the title as the collection I'm referring to is a CommandBars collection found in a Microsoft.Offic e.Interop.Outlo ok.Inspector from VSTO 2005 SE.

    In Outlook VBA I might typically have an Inspector object (equivalent to Microsoft.Offic e.Interop.Outlo ok.Inspector in C#) named VBAInspector and within that Inspector I could refer to :
    Code:
    VBAInspector.CommandBars("View")
    This would return the CommandBar object whose name was "View" (Essentially the View menu from the Inspector. An Inspector is a window opened by Outlook to show any item such as an email, contact, task, etc).

    In my C# code I have (I've tried to limit the code to relevant lines but can post more if that would be helpful) :
    Code:
    using Outlook = Microsoft.Office.Interop.Outlook;
    
    ...
        private Outlook.Inspector olInspector;
    ...
        private void olInspector_Activate()
        {
            Office.CommandBar cbView;
    
            cbView = olInspector.CommandBars("View");
        }
    This produces the following error message for line #10 :
    Code:
    'Microsoft.Office.Interop.Outlook._Inspector.CommandBars' is a 'property' but is used like a 'method'
    If I've missed anything out then just let me know and I'll be happy to provide any further information.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    Try using square brackets instead... the message is correct in that CommandBars is a property, but you're calling it like a method (ie, someClass.AMeth od("myParameter ")).

    If the collection takes a string as the index, you should be able to supply it in square brackets (like an array).

    Code:
    cbView.olInspector.CommandBars["View"];
    Been programming in a BASIC language lately? ;) (This gets me *all* the time.)

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32656

      #3
      Coming from Office and VBA, Yes. I very much have been.

      That was also the solution I needed.

      Thanks Gary :-)

      Comment

      Working...