none
Problem with mailItem.Copy(). RRS feed

  • Question

  • Hi everyone.

    When i am coding add ins for Outlook. I use this code:

    objMailItem = mailItem.Copy();

    It works fine excepts the case: When i open the saved mail item (file *.msg). It makes 2 items in outlook ( usually it should make 1 - objMailItem). So when i finished my job, i delete objMaiItem, outlook still has 1 item left.

    I try a lot but cannot fix it then i use this code:

    objMailItem = (Outlook.MailItem) application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
    objMailItem.HTMLBody = mailItem.HTMLBody;
    

    But this has the problem too. When i open the saved mail item and close it. I open it again. The window alert that thing may be open by another program (i use 3 thread in my program but i think that is not a point because with the first code, i don't have that error).

    Ah. Please help me.

    Thanks.

    Wednesday, October 3, 2012 6:16 PM

Answers

  • If the item is open but there is no ActiveInspector() it could be accessed from the Inspectors collection: Inspectors[1] for example. Of course there could be more than 1 item open.
     
    Also, I've found that if the Inspector hasn't been activated yet it will only be a weak object reference and many properties aren't filled yet. I always wait for the first Inspector.Activate() event before I do anything with the Inspector.
     
    You can always call Invalidate() on the ribbon or InvalidateControl() for a specific ribbon element in the Inspector.Activate() event as needed to fire ribbon callbacks.
     
    If you can use Redemption, an RDOMail item copy of an Outlook.MailItem won't show up in the UI unless saved. An Outlook item always shows up. I often will create an RDOMail item in the hidden Items collection in Drafts if I want to save it but not have it show up the UI.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "se7enbit" <=?utf-8?B?c2U3ZW5iaXQ=?=> wrote in message news:ae011538-9c4a-4d38-8d25-8173f5d1fe16...

    Sorry for late reply..

    The code:

    application.ActiveInspector()

    Work good only when the inspector is finished load. But the problem is i use that code when the event Ribbon.Load(). So that cause null value.

    Have any way to wait the inspector is finished load after then i run my code?

    By the way, when i use:

    objMailItem = mailItem.Copy();

    I use that because i want to modify the content of the objMailItem ( after finish job, i delete the objMaiitem).
    it create more 1 mail item to the list of inbox (when my code is not finish running).It so urgly, so... can i make the mailitem but it not show in the list of outlook?

    Thank for your time to help me.


    Ken Slovak MVP - Outlook
    • Marked as answer by se7enbit Saturday, October 6, 2012 5:47 PM
    Friday, October 5, 2012 7:48 PM

All replies

  • If you have a saved MSG file in the file system you can use CreateItemFromTemplate() to create the item directly from the MSG file.
     
    No Office application supports multi-threading. All Outlook object model code must run in the main thread. Anything else will cause Outlook to crash or hang (or for Outlook 2013 your code will be disabled) and it's not supported at all.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "se7enbit" <=?utf-8?B?c2U3ZW5iaXQ=?=> wrote in message news:a362e2a7-3666-467c-8ce3-b966f4211489...

    Hi everyone.

    When i am coding add ins for Outlook. I use this code:

    objMailItem = mailItem.Copy();

    It works fine excepts the case: When i open the saved mail item (file *.msg). It makes 2 items in outlook ( usually it should make 1 - objMailItem). So when i finished my job, i delete objMaiItem, outlook still has 1 item left.

    I try a lot but cannot fix it then i use this code:

    objMailItem = (Outlook.MailItem) application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
    objMailItem.HTMLBody = mailItem.HTMLBody;
    

    But this has the problem too. When i open the saved mail item and close it. I open it again. The window alert that thing may be open by another program (i use 3 thread in my program but i think that is not a point because with the first code, i don't have that error).

    Ah. Please help me.

    Thanks.


    Ken Slovak MVP - Outlook
    Wednesday, October 3, 2012 7:26 PM
  • Thanks.

    But how can i know the current mailItem is openned by MSG file or in the list of Out Look?

    i use this to get the current Mail Item: ( This code has a bug that when i have opening mail item, i do not close but open more item. It get wrong mail item ( it should get the second mail item but it not).

    try
                    {
                        
                        mailItem = (Outlook.MailItem)application.Inspectors[1].CurrentItem; 
                    }
                    catch (Exception)
                    {
                        try
                        {
                            Outlook.Selection selection = application.ActiveExplorer().Selection;
                            mailItem = selection[1] as Outlook.MailItem;
                        }
                        catch (Exception e)
                        {
                            
                        }
                    }


    • Edited by se7enbit Thursday, October 4, 2012 2:36 PM
    Thursday, October 4, 2012 2:30 PM
  • If an item such as a mail item is open it will be the CurrentItem for an Inspector in the Inspectors collection. If it's the active window for open items it would be in ActiveInspector().CurrentItem.
     
    If an item is not new and has been saved it would have an EntryID value. You could use NameSpace.CompareEntryIDs() to compare the id for a selected or open item with the item reference you got when you opened the MSG file.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "se7enbit" <=?utf-8?B?c2U3ZW5iaXQ=?=> wrote in message news:ac09d405-c7a2-4ff4-aca8-6f0155680289...

    Thanks.

    But how can i know the current mailItem is openned by MSG file or in the list of Out Look?

    i use this to get the current Mail Item: ( This code has a bug that when i have opening mail item, i do not close but open more item. It get wrong mail item ( it should get the second mail item but it not).

    try
                    {
                        
                        mailItem = (Outlook.MailItem)application.Inspectors[1].CurrentItem; 
                    }
                    catch (Exception)
                    {
                        try
                        {
                            Outlook.Selection selection = application.ActiveExplorer().Selection;
                            mailItem = selection[1] as Outlook.MailItem;
                        }
                        catch (Exception e)
                        {
                            
                        }
                    }



    Ken Slovak MVP - Outlook
    Thursday, October 4, 2012 2:42 PM
  • Sorry for late reply..

    The code:

    application.ActiveInspector()

    Work good only when the inspector is finished load. But the problem is i use that code when the event Ribbon.Load(). So that cause null value.

    Have any way to wait the inspector is finished load after then i run my code?

    By the way, when i use:

    objMailItem = mailItem.Copy();

    I use that because i want to modify the content of the objMailItem ( after finish job, i delete the objMaiitem).
    it create more 1 mail item to the list of inbox (when my code is not finish running).It so urgly, so... can i make the mailitem but it not show in the list of outlook?

    Thank for your time to help me.

    Friday, October 5, 2012 7:02 PM
  • If the item is open but there is no ActiveInspector() it could be accessed from the Inspectors collection: Inspectors[1] for example. Of course there could be more than 1 item open.
     
    Also, I've found that if the Inspector hasn't been activated yet it will only be a weak object reference and many properties aren't filled yet. I always wait for the first Inspector.Activate() event before I do anything with the Inspector.
     
    You can always call Invalidate() on the ribbon or InvalidateControl() for a specific ribbon element in the Inspector.Activate() event as needed to fire ribbon callbacks.
     
    If you can use Redemption, an RDOMail item copy of an Outlook.MailItem won't show up in the UI unless saved. An Outlook item always shows up. I often will create an RDOMail item in the hidden Items collection in Drafts if I want to save it but not have it show up the UI.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "se7enbit" <=?utf-8?B?c2U3ZW5iaXQ=?=> wrote in message news:ae011538-9c4a-4d38-8d25-8173f5d1fe16...

    Sorry for late reply..

    The code:

    application.ActiveInspector()

    Work good only when the inspector is finished load. But the problem is i use that code when the event Ribbon.Load(). So that cause null value.

    Have any way to wait the inspector is finished load after then i run my code?

    By the way, when i use:

    objMailItem = mailItem.Copy();

    I use that because i want to modify the content of the objMailItem ( after finish job, i delete the objMaiitem).
    it create more 1 mail item to the list of inbox (when my code is not finish running).It so urgly, so... can i make the mailitem but it not show in the list of outlook?

    Thank for your time to help me.


    Ken Slovak MVP - Outlook
    • Marked as answer by se7enbit Saturday, October 6, 2012 5:47 PM
    Friday, October 5, 2012 7:48 PM
  • Wa. I cannot use Redemption.

    But thank for your advise. My Problem is now resolved.


    • Edited by se7enbit Saturday, October 6, 2012 6:39 PM
    Saturday, October 6, 2012 5:47 PM