none
Outlook addin stop working in Windows vista RRS feed

  • Question

  • Hi friends

    I have a case here. I'm trying to send a mail using my code. when i run the below code in windows vista + office 2007 outlook, it will stop the execution after execute a particular line.

    MAPIFolder folder;
    private void Mail_Send(ref bool Cancel)
    {
    Outlook.Stores stores = this.OutlookNamespace.Stores;
                        
                        foreach (Outlook.Store store in stores)
                        {
                            folder = store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);
                            folder.Items.ItemAdd += new ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
                            
                            
                        }
    }

    After it execute this line :

    "folder = store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);"

    It will not execute any line below that code and it will not show any error. when i click the send button manually, It showing an error message "You cannot send an item that is already in the process of being sent"

    If i didnt give this line ""folder = store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);" its working fine. Outlook is not hang, we can take any other window from outlook. 

    Thanks

    Bobbin Paulose

    Wednesday, January 16, 2013 12:02 PM

Answers

  • Of course Ken is right - GetDefaultFolder was added in outlook 2010 - so your code most probably crashes or hangs. Please rewrite code to use methods available on 2007 API http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook._store_members(v=office.12).aspx 
    • Marked as answer by Bobbin Paulose Friday, January 18, 2013 12:34 PM
    Thursday, January 17, 2013 12:24 PM
  • To add to what Damian said, the code could not work in Outlook 2007 as that method doesn't exist in that version of Outlook.
     
    The property he mentioned, PR_IPM_SENTMAIL_ENTRYID, can be retrieved from the Store object, which is there in Outlook 2007, using PropertyAccessor. Something like this:
     
    string storeID = store.StoreID;
    PropertyAccessor pa = store.PropertyAccessor;
     
    // property is binary arrary, retrieve and convert to string value
    string folderID = pa.BinaryToString(pa.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x35E40102"));
     
    // olApp == Outlook.Application object.
    NameSpace ns = olApp.GetNameSpace("MAPI");
     
    Folder folder = ns.GetFolderFromID(folderID, storeID);

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "DamianD" <=?utf-8?B?RGFtaWFuRA==?=> wrote in message news:cd42ec9c-e1d3-4b60-b4ce-84d50b9822ba...

    you are saying that this code works perfectly on office 2007 on different windows versions? or maybe there is office 2010 installed?

    Anyway you could use get store and using PropertyAccessor try to access http://schemas.microsoft.com/mapi/proptag/0x35E40102 PR_IPM_SENTMAIL_ENTRYID which should be entryid of folder you are looking for


    Ken Slovak MVP - Outlook
    • Marked as answer by Bobbin Paulose Friday, January 18, 2013 12:34 PM
    Thursday, January 17, 2013 2:57 PM
    Moderator
  • 1. Do not use foreach() loops on Outlook (or other COM objects). Use indexed for() loops where you explicitly set an object that was initially declared outside the loop. That allows you to release the object you create, something that can't be done when using a foreach():
     
    Outlook.Store store = null;
    for (int i = 1: i <= stores.Count; i++)
    {
        store = stores[i];
        // whatever
        store = null;
    }
     
    You might also need to fully release objects using Marshal.ReleaseComObject() for long loops.
     
    2. Events won't fire if the objects they are attached to go out of scope. Your Items objects are all declared inside the loop and go out of scope in each loop pass and when the loop ends. For a collection of handlers you can use an Items collection that gets added to a list/collection/dictionary/etc. that will keep the Items collection alive so the event can fire. That applies to all event handling if the object for the handler goes out of scope, declare it at a level it won't go out of scope (class, globals, etc.) or add it to a list.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Bobbin Paulose" <=?utf-8?B?Qm9iYmluIFBhdWxvc2U=?=> wrote in message news:e236276e-f033-401d-ba4b-967763e3fb33...

    Hi 

    I changed my code like this

    Outlook.MAPIFolder folder;
                        Outlook.Stores stores = this.OutlookNamespace.Stores;
                        System.Collections.Generic.List<Items> AllSentItems = new System.Collections.Generic.List<Items>();
                        foreach (Outlook.Store store in stores)
                        {
                            //folder = store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);
                            //folder.Items.ItemAdd += new ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
    
                            string storeID = store.StoreID;
                            PropertyAccessor pa = store.PropertyAccessor;
                            string folderID = pa.BinaryToString(pa.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x35E40102"));
                            folder = this.OutlookNamespace.GetFolderFromID(folderID, storeID);
                            
                            Items SentItem = folder.Items;
                            AllSentItems.Add(SentItem);
    
                            SentItem.ItemAdd += new ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
                            MessageBox.Show(folder.Name.ToString());
                            //folder.
                        }


    Now its sending the mail in windows vista also. Now the problem is its not triggering "Items_ItemAdd" event.

    Thanks

    Bobbin


     

    Ken Slovak MVP - Outlook
    • Marked as answer by Bobbin Paulose Friday, January 18, 2013 12:34 PM
    Thursday, January 17, 2013 6:02 PM
    Moderator
  • you have to keep list of Items somewhere on class level so it will not get garbage collected - in your code shown here you keep them in list that will be discarded after function run.
    • Marked as answer by Bobbin Paulose Friday, January 18, 2013 12:33 PM
    Thursday, January 17, 2013 6:41 PM

All replies

  • does this code has to be done in mail send event? you can grab folders when you obtain outlook process instance for example. Second thing - you are not storing Items object, your ItemAdd events will fire only few times or event not at all.
    Wednesday, January 16, 2013 1:03 PM
  • The Store.GetDefaultFolder() method was added in Outlook 2010. Are you trying to use that method in Outlook 2007?

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Bobbin Paulose" <=?utf-8?B?Qm9iYmluIFBhdWxvc2U=?=> wrote in message news:6a9ae719-9d68-46f0-95ce-efdc64808633...

    Hi friends

    I have a case here. I'm trying to send a mail using my code. when i run the below code in windows vista + office 2007 outlook, it will stop the execution after execute a particular line.

    MAPIFolder folder;
    private void Mail_Send(ref bool Cancel)
    {
    Outlook.Stores stores = this.OutlookNamespace.Stores;
                        
                        foreach (Outlook.Store store in stores)
                        {
                            folder = store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);
                            folder.Items.ItemAdd += new ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
                            
                            
                        }
    }

    After it execute this line :

    "folder = store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);"

    It will not execute any line below that code and it will not show any error. when i click the send button manually, It showing an error message "You cannot send an item that is already in the process of being sent"

    If i didnt give this line ""folder = store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);" its working fine. Outlook is not hang, we can take any other window from outlook. 

    Thanks

    Bobbin Paulose


    Ken Slovak MVP - Outlook
    Wednesday, January 16, 2013 3:13 PM
    Moderator
  • Hi Damian

    Actually my problem is "folder = store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);" after this line the below code is not executing. 

    Items_ItemAdd

    This event is working fine. 

    Thanks

    Bobbin

    Thursday, January 17, 2013 12:11 PM
  • Of course Ken is right - GetDefaultFolder was added in outlook 2010 - so your code most probably crashes or hangs. Please rewrite code to use methods available on 2007 API http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook._store_members(v=office.12).aspx 
    • Marked as answer by Bobbin Paulose Friday, January 18, 2013 12:34 PM
    Thursday, January 17, 2013 12:24 PM
  • Hi

    Thank you for quick reply.

    My first question is why this code is not effecting in other OS. 

    Second question is is there any alternative in office 2007 for "GetDefaultFolder". It will be appreciate to past a sample code. 

    Thanks

    Bobbin

    Thursday, January 17, 2013 1:07 PM
  • you are saying that this code works perfectly on office 2007 on different windows versions? or maybe there is office 2010 installed?

    Anyway you could use get store and using PropertyAccessor try to access http://schemas.microsoft.com/mapi/proptag/0x35E40102 PR_IPM_SENTMAIL_ENTRYID which should be entryid of folder you are looking for

    Thursday, January 17, 2013 2:48 PM
  • To add to what Damian said, the code could not work in Outlook 2007 as that method doesn't exist in that version of Outlook.
     
    The property he mentioned, PR_IPM_SENTMAIL_ENTRYID, can be retrieved from the Store object, which is there in Outlook 2007, using PropertyAccessor. Something like this:
     
    string storeID = store.StoreID;
    PropertyAccessor pa = store.PropertyAccessor;
     
    // property is binary arrary, retrieve and convert to string value
    string folderID = pa.BinaryToString(pa.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x35E40102"));
     
    // olApp == Outlook.Application object.
    NameSpace ns = olApp.GetNameSpace("MAPI");
     
    Folder folder = ns.GetFolderFromID(folderID, storeID);

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "DamianD" <=?utf-8?B?RGFtaWFuRA==?=> wrote in message news:cd42ec9c-e1d3-4b60-b4ce-84d50b9822ba...

    you are saying that this code works perfectly on office 2007 on different windows versions? or maybe there is office 2010 installed?

    Anyway you could use get store and using PropertyAccessor try to access http://schemas.microsoft.com/mapi/proptag/0x35E40102 PR_IPM_SENTMAIL_ENTRYID which should be entryid of folder you are looking for


    Ken Slovak MVP - Outlook
    • Marked as answer by Bobbin Paulose Friday, January 18, 2013 12:34 PM
    Thursday, January 17, 2013 2:57 PM
    Moderator
  • Hi 

    I changed my code like this

    Outlook.MAPIFolder folder;
                        Outlook.Stores stores = this.OutlookNamespace.Stores;
                        System.Collections.Generic.List<Items> AllSentItems = new System.Collections.Generic.List<Items>();
                        foreach (Outlook.Store store in stores)
                        {
                            //folder = store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);
                            //folder.Items.ItemAdd += new ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
    
                            string storeID = store.StoreID;
                            PropertyAccessor pa = store.PropertyAccessor;
                            string folderID = pa.BinaryToString(pa.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x35E40102"));
                            folder = this.OutlookNamespace.GetFolderFromID(folderID, storeID);
                            
                            Items SentItem = folder.Items;
                            AllSentItems.Add(SentItem);
    
                            SentItem.ItemAdd += new ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
                            MessageBox.Show(folder.Name.ToString());
                            //folder.
                        }


    Now its sending the mail in windows vista also. Now the problem is its not triggering "Items_ItemAdd" event.

    Thanks

    Bobbin


     
    Thursday, January 17, 2013 5:16 PM
  • 1. Do not use foreach() loops on Outlook (or other COM objects). Use indexed for() loops where you explicitly set an object that was initially declared outside the loop. That allows you to release the object you create, something that can't be done when using a foreach():
     
    Outlook.Store store = null;
    for (int i = 1: i <= stores.Count; i++)
    {
        store = stores[i];
        // whatever
        store = null;
    }
     
    You might also need to fully release objects using Marshal.ReleaseComObject() for long loops.
     
    2. Events won't fire if the objects they are attached to go out of scope. Your Items objects are all declared inside the loop and go out of scope in each loop pass and when the loop ends. For a collection of handlers you can use an Items collection that gets added to a list/collection/dictionary/etc. that will keep the Items collection alive so the event can fire. That applies to all event handling if the object for the handler goes out of scope, declare it at a level it won't go out of scope (class, globals, etc.) or add it to a list.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Bobbin Paulose" <=?utf-8?B?Qm9iYmluIFBhdWxvc2U=?=> wrote in message news:e236276e-f033-401d-ba4b-967763e3fb33...

    Hi 

    I changed my code like this

    Outlook.MAPIFolder folder;
                        Outlook.Stores stores = this.OutlookNamespace.Stores;
                        System.Collections.Generic.List<Items> AllSentItems = new System.Collections.Generic.List<Items>();
                        foreach (Outlook.Store store in stores)
                        {
                            //folder = store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);
                            //folder.Items.ItemAdd += new ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
    
                            string storeID = store.StoreID;
                            PropertyAccessor pa = store.PropertyAccessor;
                            string folderID = pa.BinaryToString(pa.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x35E40102"));
                            folder = this.OutlookNamespace.GetFolderFromID(folderID, storeID);
                            
                            Items SentItem = folder.Items;
                            AllSentItems.Add(SentItem);
    
                            SentItem.ItemAdd += new ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
                            MessageBox.Show(folder.Name.ToString());
                            //folder.
                        }


    Now its sending the mail in windows vista also. Now the problem is its not triggering "Items_ItemAdd" event.

    Thanks

    Bobbin


     

    Ken Slovak MVP - Outlook
    • Marked as answer by Bobbin Paulose Friday, January 18, 2013 12:34 PM
    Thursday, January 17, 2013 6:02 PM
    Moderator
  • you have to keep list of Items somewhere on class level so it will not get garbage collected - in your code shown here you keep them in list that will be discarded after function run.
    • Marked as answer by Bobbin Paulose Friday, January 18, 2013 12:33 PM
    Thursday, January 17, 2013 6:41 PM
  • Hi

    Thanks Damian and Ken now its working fine. Once again thanks for your valuable comments :)

    Thanks

    Bobbin

    Friday, January 18, 2013 12:33 PM