question

MatthewWaygood-6701 avatar image
0 Votes"
MatthewWaygood-6701 asked MatthewWaygood-6701 commented

inbox of selected item (which maybe a group account)

I have a piece of someone else's code that moves emails into an archive folder, but this is moving emails from and group accounts into the personal inbox of the person performing the action. I would like to update the code to use the folder in the mailbox of the selected item, but I have way of testing this currently and i'm new to C#.

The current code is as follows:

             olNameSpace = outlook.GetNamespace("MAPI");
             inbox = olNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
             archive = inbox.Folders["MyArchive"];

             for (int i = 1; i <= selection.Count; i++)
             {
                 Outlook.MailItem mailItem = selection[i];
                 parent = mailItem.Parent;

                 if (parent.Name != "MyArchive")
                 {
                         mailItem.Move(archive);
                 }
             }

Would changing this to the following fix this issue?


             for (int i = 1; i <= selection.Count; i++)
             {
                 Outlook.MailItem mailItem = selection[i];
                 parent = mailItem.Parent;

                 olNameSpace =mailItem.Session;
                 inbox = olNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
                 archive = inbox.Folders["MyArchive"];

                 if (parent.Name != "MyArchive")
                 {
                         mailItem.Move(archive);
                 }
             }

I've remove the check for folder existing and another action that's performed, as they are irrelevant for this question. Thank you

dotnet-csharpoffice-outlook-itpro
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered MatthewWaygood-6701 commented

If you mean to move mails in a folder in one account to a folder in another account, you can try the following code:

             Application app = new Application();
    
             NameSpace outlookNs = app.GetNamespace("MAPI");
                
             var inbox = outlookNs.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
             var destFolder = (MAPIFolder)app.Application.
           ActiveExplorer().Session.Stores[1].GetDefaultFolder(OlDefaultFolders.olFolderInbox).Folders["Archive"];
            
             
             List<MailItem> ReceivedEmail = new List<MailItem>();
             for (int i = 1; i < 10; i++)
             {
                 ReceivedEmail.Add((MailItem)(inbox.Items[i]));
             }
              
             for (int i = 0; i < ReceivedEmail.Count; i++)
             {
                 MailItem mailItem = ReceivedEmail[i];
                 var  parent =(Folder)mailItem.Parent;
                 if (parent.Name != "MyArchive")
                 {
                     mailItem.Move(destFolder);
                 }
             }

We can get different accounts by using different Stores, which are limited to accounts that have been logged in to the local Microsoft Outlook application, and the order is the same as in File=> Account Information.

Did I understand what you mean correctly? If not, please add more description to let us know your question more clearly.


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

No, Sorry. It's move emails from one account into a subfolder of that same account.

The issue is that with the previous code the emails were being moved from any account the user can see into their personal accounts' subfolder. Which means others with access to the group account can no longer see that email. I'm trying to identify the Inbox that the email sits in and move it into it's subfolder.

e.g. User A can see User A inbox, Group 1 inbox and Group 2 Inbox.
They archive an email in the Group 1 inbox and it then should archive to Group 1\Inbox\MyArchive, BUT it currently going to User A\Inbox\MyArchive
User B can see User B inbox and Group 1 and Group 2 inbox, BUT they cannot see the archived mail if it's in User A's archive folder, so it needs to go into Group 1\Inbox\MyArchive

Since several emails can be selected at once, I need to determine the inbox of each item individually.

0 Votes 0 ·

Do we have to use mailItem.Movemethod?
Is it possible to use the MailItem.Copy method?

0 Votes 0 ·

We could use copy, but that could mean that several people will then perform the same action on the one mail item.
Move is better, as it indicates that the task has been completed and the mail item is archived off.

I really just need to know, if this code will determine the archive folder to use for the mail item:

          olNameSpace = mailItem.Session; # <- Does this target the account where the mail item is currently located
          inbox = olNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);  # <- then this would be the accounts Inbox
          archive = inbox.Folders["MyArchive"]; # <- then this would be the accounts Inbox\MyArchive

The old code uses 'outlook' which is the current users account.

          olNameSpace = outlook.GetNamespace("MAPI");

Session seems to be a strange name to give this method, https://docs.microsoft.com/en-us/office/vba/api/outlook.mailitem.session

0 Votes 0 ·
Show more comments