Is there a way to have Outlook VSTO read a text file?

Viewed 53

I am using the language c#. I got Outlook to run through VSTO, but cannot seem to have the text file I want Outlook to read run correctly. The text file consists a list of names, which I want to have it automatically look up through Outlook to give me a statement of true or false if there is a match for the name in Outlook from the textfile. I want it to read the text file by row. Here is my code so far:

namespace OutlookAddIn1
{
    public partial class ThisAddIn
    {
        private static int Main(string[] args)


        {

            System.Diagnostics.Debug.WriteLine("hello");
            Console.WriteLine("test");
            string filePath = @"C:\Users\Desktop\QC\User_.txt"; 
            string filePath2 = @"C:\Users\Documents\QC\userlist.txt";
            List<string> lines = File.ReadAllLines(filePath).ToList();
            Console.WriteLine(lines);
            foreach (string line in lines)
            {
                Console.WriteLine(line);
            }
            lines.Add("True");

            List<string> list = new List<string>();
            string inSystem = "";
            lines = File.ReadAllLines(filePath).ToList();
            using (StreamWriter sw = new StreamWriter(filePath)) ;
            string OutLook_Username_Output = "";
            foreach (string line in lines)
            {
                if (line.Equals(OutLook_Username_Output))
                {
                    inSystem += "true" + Environment.NewLine;
                }
                else
                {
                    inSystem += "false" + Environment.NewLine;
                }
                System.Diagnostics.Debug.WriteLine("true");
                Console.WriteLine(line);
                for (int i = 0; i < lines.Count; i++)
                {
                    Console.WriteLine(lines[i] + ":" + inSystem[i]);
                }
                

             

                File.WriteAllText(@"C:\Users\059974\Documents\QC\userlist.txt",inSystem );
                return 0;

            }



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

                        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
                        {
                            // Note: Outlook no longer raises this event. If you have code that 
                            //    must run when Outlook shuts down, see https://go.microsoft.com/fwlink/?LinkId=506785
                        }
           
            #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);
                        }
           
            return 0;
            #endregion
        }
    }
2 Answers

If I understand your question correctly, you need to call Application.Session.CreateRecipient / Recipient.Resolve. If Recipient.Resolve returns true, the name is resolved and can be used to send an email to. Note than anything that looks like an SMTP address ("something@whatever.blah") will be resolved to a one-off recipient even if it is not in the Outlook address book.

Something along the lines of

Recipient recip = Globals.ThisAddIn.Application.Session.CreateRecipient(line);
if (recip.Resolve())
{
    //the name in the line variable can be resolved 
}

The NameSpace.CreateRecipient method creates a Recipient object. The name of the recipient can be a string representing the display name, the alias, or the full SMTP email address of the recipient. This method is most commonly used to create a Recipient object for use with the GetSharedDefaultFolder method, for example, to open a delegator's folder. It can also be used to verify a given name against an address book like in your scenario.

To make sure that such name exists in the address book you need to use the Recipient.Resolve method. It attempts to resolve a Recipient object against the Address Book and returns true if the object was resolved; otherwise, false. So, if it returns false - there is no such record in Outlook for a given name.

var recip = session.CreateRecipient("Eugene Astafiev");
if(recip.Resolve())
{
   // the entry was found in outlook
}
else
{
  // the specified person was not found in the Outlook's address book
}
Related