【问题标题】:Is there a way to have Outlook VSTO read a text file?有没有办法让 Outlook VSTO 读取文本文件?
【发布时间】:2022-08-04 21:23:47
【问题描述】:

我正在使用语言 c#。我让 Outlook 通过 VSTO 运行,但似乎无法让我希望 Outlook 正确读取的文本文件运行。文本文件包含一个名称列表,如果 Outlook 中的名称与文本文件中的名称匹配,我想让它通过 Outlook 自动查找它,以便给我一个真或假的陈述。我希望它逐行读取文本文件。到目前为止,这是我的代码:

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
        }
    }
  • 为什么需要在 VSTO 加载项中使用 Main 方法?
  • 我不确定,我用什么来代替它?
  • 通常,当加载项启动时会调用 ThisAddin 类的 Startup 方法(请参阅代码中的 ThisAddIn_Startup)。
  • 那么我是否应该将我拥有的所有代码放在 Main 下并将其放在 ThisAddIn_Startup 下,然后删除 Main ?
  • 如果您不使用加载项的事件处理程序,您的代码将不会运行。

标签: c# outlook vsto outlook-addin office-addins


【解决方案1】:

如果我正确理解您的问题,您需要致电Application.Session.CreateRecipient / Recipient.Resolve。如果Recipient.Resolve 返回true,则名称已解析并可用于向其发送电子邮件。请注意,任何看起来像 SMTP 地址 ("something@whatever.blah") 的内容都将被解析为一次性收件人,即使它不在 Outlook 通讯簿中也是如此。

类似的东西

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

【讨论】:

  • 如何调用 Application.Session.CreateRecipient?我对编程很陌生,所以我不明白在代码中的哪里输入。
  • 请参阅上面的更新答案。
【解决方案2】:

NameSpace.CreateRecipient 方法创建一个 Recipient 对象。收件人的名称可以是一个字符串,表示收件人的显示名称、别名或完整的 SMTP 电子邮件地址。此方法最常用于创建与GetSharedDefaultFolder 方法一起使用的收件人对象,例如,打开委托人的文件夹。它还可用于根据您的方案中的地址簿验证给定名称。

要确保这样的名称存在于通讯簿中,您需要使用Recipient.Resolve 方法。它尝试根据地址簿解析Recipient 对象,如果对象已解析,则返回true;否则为假。因此,如果它返回 false - Outlook 中没有给定名称的此类记录。

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
}

【讨论】:

  • NameSpace 在代码中的位置。我很困惑
  • 您可以使用GetNamespace("MAPI") 方法或Session 属性来检索命名空间类实例。
  • 我仍然在合并命名空间时遇到问题。
  • 请使用示例代码检查更新后的帖子。
猜你喜欢
  • 1970-01-01
  • 2011-04-20
  • 2020-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多