none
OutlookItem.Parent returns the wrong folder after move item RRS feed

  • Question

  • I move a mail from the Inbox to another custom folder and after that I use the function OutlookItem.Parent to get the new folder, but this function still giving the "Inbox".

    There are a way to refresh this email parent data?

    Thanks& Regards,

    Rui Oliveira

    Wednesday, June 7, 2017 5:21 PM

All replies

  • Hello Rui,

    I think there is no way to refresh the item. We need to reprocess to get the moved item, then its parent data would be updated.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, June 8, 2017 8:03 AM
    Moderator
  • What you mean with "reprocess"?
    Thursday, June 8, 2017 8:54 AM
  • Hi Rui,

    I suggest you re-capture the mail item and then try to return its parent. According to MailItem.Move, the method returns an object represents the item which has been moved, so you could handle like this

    Debug.Print "Parent Foler before moving:" & mail.Parent
    Set mail = mail.Move(destFolder) 'move to target folder
    Debug.Print "Parent Foler after moving:" & mail.Parent 

    Best Regards,

    Terry

    Friday, June 9, 2017 8:27 AM
  • Hi Rui,

    I suggest you re-capture the mail item and then try to return its parent. According to MailItem.Move, the method returns an object represents the item which has been moved, so you could handle like this

    Debug.Print "Parent Foler before moving:" & mail.Parent
    Set mail = mail.Move(destFolder) 'move to target folder
    Debug.Print "Parent Foler after moving:" & mail.Parent 

    Best Regards,

    Terry

    Hi Terry,

    That is exactly the point. After the user moves to a new folder, the "mail.Parent" still giving the parent folder before moving.

    Thanks anyway,

    Rui

    Friday, June 9, 2017 8:53 AM
  • Hi Rui,

    How do you move the mail? Which version of Office and OS are you using? Would you mind share us your code so that we could try to reproduce your issue. Will you get the same results on another computer?

    Best Regards,

    Terry

    Monday, June 12, 2017 4:56 AM
  • Hi Rui,

    How do you move the mail? Which version of Office and OS are you using? Would you mind share us your code so that we could try to reproduce your issue. Will you get the same results on another computer?

    Best Regards,

    Terry

    Hi Terry,

    I am using Windows7 32bits and Office2013.

    The email is moved manually by the user from one folder to another folder. After that I select the email and with right click popup menu, call the code that will get the Parent.

    Repeat more than once this steps, because sometimes shows the correct folder other times shows the wrong folder (shows the location before moves). Steps: i) Move the email to a new folder; ii) Right click the function from popup menu to see the folder that “Parent” returns.

    Here is the code:

            public void OnRegisterButtonClick(Office.IRibbonControl control)

            {

                if (control.Context is Outlook.Selection)

                {

                    Outlook.Selection selection =

                        control.Context as Outlook.Selection;

                    if (selection.Count == 1)

                    {

                        OutlookItem olItem =

                            new OutlookItem(selection[1]);

                        Outlook.Folder folder = null;

                        folder = olItem.Parent;

                        MessageBox.Show(folder.FolderPath);

      }

                }

            }

    Thanks & Regards,

    Rui

    Monday, June 12, 2017 3:29 PM
  • Hi Rui,

    I failed to reproduce your issue. What's the OutlookItem? Is it a customize Class? For getting the selection item, you could use selection[1]. I modified the code and test about 30 times and didn't get original path. Hope this code could help you.

    if (control.Context is Outlook.Selection)
                {
                    Outlook.Selection selection =
                        control.Context as Outlook.Selection;
                    if (selection.Count == 1)
                    {                  
                        Outlook.Folder folder = null;
                        folder = selection[1].Parent;
                        MessageBox.Show(folder.FolderPath);
                    }
                }

    Best Regards,

    Terry

    Wednesday, June 14, 2017 5:05 AM
  • Hi Rui,

    I failed to reproduce your issue. What's the OutlookItem? Is it a customize Class? For getting the selection item, you could use selection[1]. I modified the code and test about 30 times and didn't get original path. Hope this code could help you.

    if (control.Context is Outlook.Selection)
                {
                    Outlook.Selection selection =
                        control.Context as Outlook.Selection;
                    if (selection.Count == 1)
                    {                  
                        Outlook.Folder folder = null;
                        folder = selection[1].Parent;
                        MessageBox.Show(folder.FolderPath);
                    }
                }

    Best Regards,

    Terry


    Hi Terry,

    I am using Outlook 2013 from Office 2013 Professional.

    To start, I do not understand how you could run your code?

    folder = selection[1].Parent;

    Selection does not have the property Parent, just the MailItem have this property. For do it you must do it like in my code:

    OutlookItem olItem = new OutlookItem(selection[1]);

    Outlook.Folder folder = null;

    folder = olItem.Parent;

    MessageBox.Show(folder.FolderPath);

    Wednesday, June 14, 2017 8:25 AM
  • Hi Rui,

    Have you tried my code and did you get any error? In fact, Selection has Parent property and so it works for me. I failed to use your code  because I didn't find the OutlookItem class. What is the OutlookItem?

    I'm using Microsoft.Office.Interop.Outlook.dll now and which dll are you using?

    Best Regards,

    Terry

    Thursday, June 15, 2017 8:31 AM
  • Hi Rui,

    Have you tried my code and did you get any error? In fact, Selection has Parent property and so it works for me. I failed to use your code  because I didn't find the OutlookItem class. What is the OutlookItem?

    I'm using Microsoft.Office.Interop.Outlook.dll now and which dll are you using?

    Best Regards,

    Terry

    I am using too "Microsoft.Office.Interop.Outlook.dll" in VStudio 2010.

    C:\Program Files\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14\Microsoft.Office.Interop.Outlook.dll

    Friday, June 16, 2017 9:19 AM
  • Hi Rui,

    I suggest you share your add-in project so we could try to reproduce your issue. You could share the project will OneDrive and put link here. I will spend some time to create a virtual machine with same testing environment(VS,OS,OFFICE) with you and then create an add-in project to share with you too. Please be patient and wait for my result.

    Best Regards,

    Terry

    Friday, June 16, 2017 10:52 AM
  • Hi,

    Here is the link for the sample code:

    https://1drv.ms/u/s!AgmZaoAIT_6agSR8sM1cm_VpaaQf

    I am working with: Windows 7 32bits, Office 2013 and VStudio 2010.

    Friday, June 16, 2017 1:03 PM
  • Hi Rui,

    Sorry for late reply, I have tried to test your project. I failed to run the project, maybe I missed some references, but obviously OutlookItem is a custom class. I want to create a project to you but in fact I couldn't create VSTO project for Office 2013 using Visual Studio 2010. I think Selection.Parent could return the parent folder without using a custom class. I suggest you create a new project with my code in the previous reply for testing. Selection.Parent could not be associated by a "." character, but it could be compiled by vs. Did you get any error message when using selection[1].Parent? Here is a example project created by visual studio 2010 and works on outlook 2010, you could take reference for creating the new project. https://1drv.ms/u/s!ArC0gnwxLv5qhiGcgY9PjDKC1KEg

    Best Regards,

    Terry

    Wednesday, June 21, 2017 1:04 AM
  • Hi Rui,

    Sorry for late reply, I have tried to test your project. I failed to run the project, maybe I missed some references, but obviously OutlookItem is a custom class. I want to create a project to you but in fact I couldn't create VSTO project for Office 2013 using Visual Studio 2010. I think Selection.Parent could return the parent folder without using a custom class. I suggest you create a new project with my code in the previous reply for testing. Selection.Parent could not be associated by a "." character, but it could be compiled by vs. Did you get any error message when using selection[1].Parent? Here is a example project created by visual studio 2010 and works on outlook 2010, you could take reference for creating the new project. https://1drv.ms/u/s!ArC0gnwxLv5qhiGcgY9PjDKC1KEg

    Best Regards,

    Terry

    Hi Terry,

    I do not need to change anything in your project. Exactly as you send it... I can compile it and run/debug it in Office2013, and the with your project I can verify the problem selecting your menu "Test".

    (the only change have been write "C:\Program Files\Microsoft Office\Office15\OUTLOOK.EXE" in "Debug/Start action" to start the Outlook in debug)

    Do the following steps:

    1) Create 3 custom folders "AAA", "BBB" and "CCC";

    2) With drag&drop move an email to the folder "AAA", select the email moved to "AAA" and click your menu "Test". The dialog shows the correct path? If yes, go to the next step;

    3) Again with drag&drop move the email from "AAA" to the folder "BBB", select the email moved to "BBB" and click your menu "Test". The dialog shows the correct path? If yes, go to the next step;

    4) Again with drag&drop move the email from "BBB" to the folder "CCC", select the email moved to "CCC" and click your menu "Test". The dialog shows the correct path?

    For me in the most of times gives the wrong path.

    Let me know your results.

    Rui




    Monday, June 26, 2017 5:00 PM
  • Hi Rui,

    As you can see, it works for me. I suggest you test the project on another PC or you could try to repair your office.

    Best Regards,

    Terry



    Wednesday, June 28, 2017 11:41 AM
  • Ola Rui

    it is a stupid Outlook mistake...

    If you do oMailItem.Save before you use it, it will be correct.

    Cheers Jörg

    Monday, January 1, 2018 8:44 PM
  • Hi Jörg,

    Thanks for the reply.

    I have tested your suggestion but in my case save the email didn't solved.

    Outlook.Folder folder = null; folder = olItemAux.Parent; MessageBox.Show("Parent Folder after moving1:" + folder.FolderPath); olItemAux.Save(); folder = olItemAux.Parent; MessageBox.Show("Parent Folder after moving2:" + folder.FolderPath);

    Thanks & Regards,

    Rui



    Tuesday, January 9, 2018 6:20 PM