Access Outlook 2010 mail folder in C #

I am working on an Outlook add-in and have recently switched to C # for dating (I am a Java person at heart). At this point, I'm just trying to iterate through the mail folder and print the subject of each message on the console, mainly as a way to make sure everything is working fine so far. However, when I run it, I get the following error:

The operation failed. One or more parameter values ​​are invalid.

Exception Text:

System.ArgumentException: Operation failed. One or more parameter values ​​are not valid. in Microsoft.Office.Interop.Outlook.NameSpaceClass.GetFolderFromID (String EntryIDFolder, Object EntryIDStore) in OutlookAddIn2.ThisAddIn.ThisAddIn_Startup (object sender, EventArgs e) in Microsoft.Office.Tools.AddInuppl.OnStartOffart.Onartartffff .AddInImpl.AddInExtensionImpl.Microsoft.Office.Tools.EntryPoint.OnStartup () in Microsoft.Office.Tools.AddInBase.OnStartup () in OutlookAddIn2.ThisAddIn.FinishInitialization () in Microsoft.Office.Tools.Mdice.Off.Off.Base .EntryPoint.FinishInitialization () in Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.ExecutePhase (ExecutionPhases executionPhases) in Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.Microsoft.VisualStudio.Tools.Office.Runtime.Interop.IExecuteCustomization2.ExecuteEntryPoints ()

:

, , Microsoft MSDN, . , , , . , , , , !

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
    }
}
+5
4

:

 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;
        }    
+1

, pickFolder.EntryID pickFolder.StoreID. EntryID StoreID .

Trace.TraceInformation("EntryID: {0}\tStoreID: {1}", pickFolder.EntryID, pickFolder.StoreID);

, pickFolder null, .

, , GetFolderFromID - .

0

, , "":

Outlook.Application app = new Outlook.Application();

Outlook.NameSpace ns = app.GetNamespace("MAPI");

Outlook.Folder folder = app.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;
0

It is not clear if you passed the code in the debugger, as @SilverNinja pointed out. Validation of StoreID and EntryID is important.

There are several more options:

Your Outlook PST is slightly damaged. Try scanPST and see if that helps.

Also, you might think that the pickFolder enumeration would be smart enough to skip them, but do you have other items at the top level of the folder tree than folders? I had this problem with the enumeration of contacts and the presence of contactless elements in the contacts folder.

0
source

All Articles