none
InvokeMember Not Working RRS feed

  • Question

  • PRE-REQUISITES:
    You have delegate access to someone's folder

    RECREATE STEPS:
    1.  Create a meeting in a Calendar for which you have delegate Access
    2.  Double click on the meeting to open it in an Inspector Window
    3.  Click on the button "Copy To My Calendar"
    4.  Select "Accept Meeting"

    EVENTS THAT FIRE IN SEQUENCE
    ItemAdd (IDispatch)
    ItemChange (IDispatch)
    ItemAdd (IDispatch)
    ItemChange (IDispatch)
    ItemRemove ()
    ItemChange (IDispatch)

    The first time ItemAdd fires InvokeMember throws an exception (see message).  However, the 2nd time through the function works fine.  Why is that?



    Exception has been thrown by the target of an invocation.   at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
       at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
       at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args)

            public override void ItemAdd(object item)
            {          
              
                Outlook._AppointmentItem _apptItem = null;
    
                try
                {
                    if (item is Outlook._AppointmentItem)
                    {
    
                        // This call fails the first time, but works fine the 2nd time this event is fired.
         
                        string gid = (string)item.GetType().InvokeMember("GlobalAppointmentID", System.Reflection.BindingFlags.GetProperty, null, item, null);
    
                        
                    }
                }
                   
                catch( Exception ex0)
                {
                    // Process Exception
                }
    
                
    }
    
    Wednesday, March 16, 2016 5:32 PM

Answers

  • Hi GermanH,

    As our tests, ItemAdd will fire twice when a new calendar is added. First time, it will create a temporary calendar in calendar items, then remove it. Second time, it will create a real calendar in calendar items. So, I suggest you get the new calendar from calendar items instead of item in ItemAdd event.

    Here is a simple code:

            internal Outlook.Items items;
            internal Outlook.AppointmentItem appointmentItem;
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                items = Application.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar).Items;
                items.ItemAdd += items_ItemAdd;
            }
    
            void items_ItemAdd(object Item)
            {
                appointmentItem = items.GetLast() as Outlook.AppointmentItem;
                string sub = appointmentItem.Subject;
                var status = appointmentItem.MeetingStatus;            
            }

    Best Regards,

    Edward

           

    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.


    • Marked as answer by GermanEZI Thursday, March 17, 2016 3:21 PM
    Thursday, March 17, 2016 8:11 AM

All replies

  • Do you get the same error if you don't use reflection?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, March 16, 2016 5:44 PM
  • I get the following:

    outlook.exe Error: 0 : Exception: [ItemAdd] The operation failed.   at Microsoft.Office.Interop.Outlook._AppointmentItem.get_GlobalAppointmentID()
       at OtlTracker.ThisAddIn._folderItems_ItemAdd(Object Item) in c:\Code\OtlTracker\OtlTracker\ThisAddIn.cs:line 54

    void _folderItems_ItemAdd(object Item) { System.Diagnostics.Trace.TraceInformation("_folderItems_ItemAdd"); Outlook._AppointmentItem apptItem = null; try { if (Item is Outlook._AppointmentItem) { apptItem = (Outlook._AppointmentItem)Item;
    // Again, the Exception is thrown the 1st time "ItemAdd" is fired.

    // The second time, accessing the property works just fine. string gid = apptItem.GlobalAppointmentID; } } catch( Exception ex0) { System.Diagnostics.Trace.TraceError( "Exception: [ItemAdd] " + ex0.Message + ex0.StackTrace); return; } }



    • Edited by GermanEZI Wednesday, March 16, 2016 6:09 PM Clarification
    Wednesday, March 16, 2016 6:07 PM
  • Can you look at the exception details, in particular the error code?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, March 16, 2016 6:10 PM
  • FYI:  I'm using OutlookSpy to see the events fired when "Copy To My Calendar" is clicked.
    Wednesday, March 16, 2016 6:10 PM
  • 0xFFE40107

    COMException:
    outlook.exe Error: 0 : Exception: [ItemAdd] The operation failed.   at Microsoft.Office.Interop.Outlook._AppointmentItem.get_GlobalAppointmentID()
       at OtlTracker.ThisAddIn._folderItems_ItemAdd(Object Item) in c:\Code\OtlTracker\OtlTracker\ThisAddIn.cs:line 54
    Wednesday, March 16, 2016 6:33 PM
  • Hi GermanH,

    As our tests, ItemAdd will fire twice when a new calendar is added. First time, it will create a temporary calendar in calendar items, then remove it. Second time, it will create a real calendar in calendar items. So, I suggest you get the new calendar from calendar items instead of item in ItemAdd event.

    Here is a simple code:

            internal Outlook.Items items;
            internal Outlook.AppointmentItem appointmentItem;
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                items = Application.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar).Items;
                items.ItemAdd += items_ItemAdd;
            }
    
            void items_ItemAdd(object Item)
            {
                appointmentItem = items.GetLast() as Outlook.AppointmentItem;
                string sub = appointmentItem.Subject;
                var status = appointmentItem.MeetingStatus;            
            }

    Best Regards,

    Edward

           

    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.


    • Marked as answer by GermanEZI Thursday, March 17, 2016 3:21 PM
    Thursday, March 17, 2016 8:11 AM
  • Thanks!  I will try this immediately and get back to you.
    Thursday, March 17, 2016 3:10 PM
  • That SOLVED my issue.  Thanks!!

    One last question.  Should I run ReleaseComObject() on the Item returned once I'm done with it?

    I would imagine so.
    • Edited by GermanEZI Thursday, March 17, 2016 3:23 PM Grammar
    Thursday, March 17, 2016 3:22 PM
  • Hi GermanH,

    >> Should I run ReleaseComObject() on the Item returned once I'm done with it?

    I think you could.

    Best Regards,

    Edward


    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.


    Friday, March 18, 2016 4:58 AM