Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Outlook 2010 Mail Folder in C#

Tags:

c#

outlook

add-in

I am working on an Outlook Add-In, and recently switched to C# for sake of familiarity (I am a Java man at heart). At this point, I am simply trying to iterate through a mail folder and print the subject of each message to the console, mainly as a way to make sure everything is working alright so far. Whenever I run it, however, I receive the following error:

Could not complete the operation. One or more parameter values are not valid.

Exception Text:

System.ArgumentException: Could not complete the operation. One or more parameter values are not valid. at Microsoft.Office.Interop.Outlook.NameSpaceClass.GetFolderFromID(String EntryIDFolder, Object EntryIDStore) at OutlookAddIn2.ThisAddIn.ThisAddIn_Startup(Object sender, EventArgs e) at Microsoft.Office.Tools.AddInImpl.OnStartup() at Microsoft.Office.Tools.AddInImpl.AddInExtensionImpl.Microsoft.Office.Tools.EntryPoint.OnStartup() at Microsoft.Office.Tools.AddInBase.OnStartup() at OutlookAddIn2.ThisAddIn.FinishInitialization() at Microsoft.Office.Tools.AddInBase.Microsoft.Office.Tools.EntryPoint.FinishInitialization() at Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.ExecutePhase(ExecutionPhases executionPhases) at Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.Microsoft.VisualStudio.Tools.Office.Runtime.Interop.IExecuteCustomization2.ExecuteEntryPoints()

Loaded Assemblies:

I am somewhat baffled by this, as this is the precise method recommended by Microsoft on MSDN to have a user select a folder. I have included my source, please let me know if you have any thoughts. Thanks for taking the time to read this post, and for being willing to help out!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;

namespace OutlookAddIn2
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            //Get application namespace and grab the original folder object
            Outlook.Folder pickFolder = (Outlook.Folder)Application.Session.PickFolder();

            //Outlook.Folder mrFolder = Application.Session.GetFolderFromID(pickFolder.EntryID, pickFolder.StoreID) as Outlook.Folder;

            foreach (Outlook.MailItem oMailItem in pickFolder.Items)
            {
                Console.WriteLine(oMailItem.Subject);
            }
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }
}
like image 431
flyingscotsman74656 Avatar asked Nov 13 '22 02:11

flyingscotsman74656


1 Answers

Tray this:

 public static Folder FOLDER_1;
 public static Folder FOLDER_2;
 public static Folder FOLDER_N;

/// <summary>
        /// Hilo que lee el archivo de datos PST del OUTLOOK
       private static void readPst()
        {
            try
            {
                Application app = new Application();
                NameSpace outlookNs = app.GetNamespace("MAPI");
                MAPIFolder mf = outlookNs.GetDefaultFolder(OlDefaultFolders.olFolderTasks);


                string names = mf.FolderPath.Split('\\')[2];



                Folder fMails = getFolder(fCarpetasPersonales.Folders, "Inbox");



                FOLDER_1= getFolder(fMails.Folders, "FOLDER_1");
                FOLDER_2= getFolder(fMails.Folders, "FOLDER_2");
                FOLDER_N= getFolder(fMails.Folders, "FOLDER_n");

//TO DO... For example:  foreach (object item in fMails.Items)



     private static Folder getFolder(Folders folders, string folder)
        {
            foreach (object item in folders)
            {
                if (item is Folder)
                {
                    Folder f = (Folder)item;
                    if (f.Name.Equals(folder))
                    {
                        return f;
                    }
                }
            }
            return null;
        }    
like image 152
RTGROUP Avatar answered Dec 15 '22 10:12

RTGROUP