首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何:以编程方式确定新事件处理程序的当前Outlook项

如何:以编程方式确定新事件处理程序的当前Outlook项
EN

Stack Overflow用户
提问于 2022-10-21 20:28:27
回答 2查看 73关注 0票数 0

如主题所示,我正在尝试确定Outlook项是否为会议请求,同时创建一个新的.ItemSend事件处理程序。我已经和探长和探险家一起研究过这样做的方法。

我曾检讨过:

https://learn.microsoft.com/en-us/visualstudio/vsto/how-to-programmatically-determine-the-current-outlook-item?view=vs-2022&tabs=csharp

这是:

ActiveExplorer().Selection returns previously selected mail in Outlook C#

还有一些类似的问题。

我已经成功地建立了一个新的检查器事件处理程序,并为Application.ItemSend定义了事件处理程序,这些处理程序非常有效。但只有当我发出会面请求的时候。定期的电子邮件响应事件触发器,但重新启动Outlook,将电子邮件保存为草稿,而不发送。

我不确定如何结合使用审查员和浏览器来防止Outlook崩溃?我不确定我能不能只用检查员来做这件事?只使用资源管理器?

我相信,如果我能够将所有这些代码包装为将outlook项标识为MailItem或会议请求,我就可以通过电子邮件和新的事件处理程序来解决这个问题。

正确的方向上的一点是值得赞赏的。-谢谢--克里斯

代码语言:javascript
运行
复制
    public partial class ThisAddIn
    {
        private Outlook.Inspectors inspectors = null;
        private Outlook.UserProperty objUserPropertyEventId = null;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            inspectors = this.Application.Inspectors;
            inspectors.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
            // define event handler for ItemSend
            Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
        }

        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
        }

        void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
        {
            // <testing this>
            Outlook.Selection sel = Application.ActiveExplorer().Selection;

            if (sel.Count == 1)
                MessageBox.Show("One selected");

            //Outlook.MailItem mail = sel[1] as Outlook.MailItem;
            Object selObject = this.Application.ActiveExplorer().Selection[1];

            //if (mail != null)
            if (selObject is Outlook.MailItem)
                MessageBox.Show("This is a mail item."/*\n\n" + mail.Subject*/);
            else
                MessageBox.Show("This is NOT a mail item."/*\n\n" + mail.Subject*/);
            // </testing this>

            // <this works, but not for email.  Only meeting requests.>
            #pragma warning disable IDE0019 // Use pattern matching
            Outlook.AppointmentItem appt = Inspector.CurrentItem as Outlook.AppointmentItem;
            #pragma warning restore IDE0019 // Use pattern matching

            // is it an AppointmentItem
            if (appt != null)
            {
                MessageBox.Show("appt != null");
                // is it a meeting request
                if (appt.MeetingStatus == Outlook.OlMeetingStatus.olMeeting)
                {
                    // force time of initial meeting dialog to be 11:29pm
                    // to avoid immediate reminder of upcoming meeting
                    DateTime datDateTime = DateTime.Now;
                    
                    string strYear = datDateTime.ToString().Substring(6, 4);
                    int intYear;
                    intYear = Int32.Parse(strYear);

                    string strMonth = datDateTime.ToString("MM/dd/yyyy").Substring(0, 2);
                    int intMonth;
                    intMonth = Int32.Parse(strMonth);

                    string strDay = datDateTime.ToString("MM/dd/yyyy").Substring(3, 2);
                    int intDay;
                    intDay = Int32.Parse(strDay);

                    string strReqAtt = appt.RequiredAttendees;
                    if (string.IsNullOrEmpty(strReqAtt))
                        appt.Start = new DateTime(intYear, intMonth, intDay, 23, 29, 00);

                    // save to generate EntryId for future reference
                    appt.Save();

                    // save EntryId as UserProperty
                    Outlook.AppointmentItem mtg;
                    mtg = (Outlook.AppointmentItem)Inspector.CurrentItem;

                    if (mtg != null)
                    {
                        if (mtg is Outlook.AppointmentItem)
                        {
                            mtg.MeetingStatus = Outlook.OlMeetingStatus.olMeeting;
                            string strEntryId = mtg.EntryID;

                            Outlook.UserProperties objUserProperties = mtg.UserProperties;
                            objUserPropertyEventId = objUserProperties.Add("MeetingEntryId", Outlook.OlUserPropertyType.olText, true, 1);
                            objUserPropertyEventId.Value = strEntryId;
                        }
                    }
                    if (mtg != null)
                        Marshal.ReleaseComObject(mtg);
                }
                if (appt != null)
                    Marshal.ReleaseComObject(appt);
            }
        }

        public void Application_ItemSend(object Item, ref bool Cancel)
        {
            // use EventId to identify current meeting request
            var app = new Microsoft.Office.Interop.Outlook.Application();
            var ns = app.Session;

            Outlook.AppointmentItem meeting = ns.GetItemFromID(objUserPropertyEventId.Value);
            MessageBox.Show(meeting.Subject);
            //if (meeting.MeetingStatus != Outlook.OlMeetingStatus.olNonMeeting)
            if (meeting.MeetingStatus == Outlook.OlMeetingStatus.olMeeting)
            {
                // is ItemSend a request or a cancel
                if (meeting.MeetingStatus != Outlook.OlMeetingStatus.olMeetingCanceled)
                {
                    MessageBox.Show("MeetingStatus != olMeetingCanceled");
                    Outlook.Recipient recipConf = null;
                    try
                    {
                        // ItemSend is a request
                        if (meeting is Outlook.AppointmentItem)
                        {
                            //if (meeting.MeetingStatus == Outlook.OlMeetingStatus.olMeeting)
                            {
                                Outlook.Recipient recipRoomUser;
                                // if a location was provided
                                if (meeting.Location != null)
                                {
                                    string strLocation = meeting.Location;
                                    bool blnTorF = false;
                                    string strConference = "|";

                                    // Clears lazy room number typing
                                    // Location resource:
                                    // places meeting on 'JHP Conference ###' calendar
                                    // and
                                    // sends email to conference room user with link for Teams meeting
                                    //      conference room user is added to 'Location' by including email in 'Required'
                                    if (!strLocation.Contains("Room - Conference"))
                                        meeting.Location = "";

                                    // Add calendar users (based on room location) to email
                                    // sends email to conference room user with link for Teams meeting
                                    //      conference room user is added to previously cleared 'Location'
                                    //      by including email in 'Required'
                                    if (strLocation.Contains("142"))
                                    {
                                        recipRoomUser = meeting.Recipients.Add("xxx@xxx.com");
                                        recipRoomUser = meeting.Recipients.Add("xxx@xxx.com");
                                        recipRoomUser.Type = (int)Outlook.OlMeetingRecipientType.olRequired;
                                        if(meeting.Location == "")
                                            meeting.Location = "xxx@xxx.com";
                                    }
                                    if (strLocation.Contains("150"))
                                    {
                                        recipRoomUser = meeting.Recipients.Add("xxx@xxx.com");
                                        recipRoomUser = meeting.Recipients.Add("xxx@xxx.com");
                                        recipRoomUser.Type = (int)Outlook.OlMeetingRecipientType.olRequired;
                                        if (meeting.Location == "")
                                            meeting.Location = "xxx@xxx.com";
                                    }
                                    if (strLocation.Contains("242"))
                                    {
                                        recipRoomUser = meeting.Recipients.Add("xxx@xxx.com");
                                        recipRoomUser = meeting.Recipients.Add("xxx@xxx.com");
                                        recipRoomUser.Type = (int)Outlook.OlMeetingRecipientType.olRequired;
                                        if (meeting.Location == "")
                                            meeting.Location = "xxx@xxx.com";
                                    }
                                    if (strLocation.Contains("248"))
                                    {
                                        recipRoomUser = meeting.Recipients.Add("xxx@xxx.com");
                                        recipRoomUser = meeting.Recipients.Add("xxx@xxx.com");
                                        recipRoomUser.Type = (int)Outlook.OlMeetingRecipientType.olRequired;
                                        if (meeting.Location == "")
                                            meeting.Location = "xxx@xxx.com";
                                    }
                                    MessageBox.Show("Location assigned");

                                    // build string of recipients for .Split('|') to array
                                    foreach (Outlook.Recipient objUser in meeting.Recipients)
                                    {
                                        // remove previous location prior to room change
                                        if (!objUser.Name.Contains("JHP Conference") && !objUser.Name.Contains("Room - Conference"))
                                            if (!strConference.Contains(objUser.Name)) // no duplicates
                                                strConference = strConference + objUser.Name + "|";
                                    }

                                    if (strConference != "|")
                                    {
                                        // create array from string without duplicates
                                        //MessageBox.Show("strConference != |\n\n" + strConference);
                                        string[] arrConference = null;
                                        strConference = strConference.TrimStart('|');
                                        arrConference = strConference.Split('|');

                                        // verify array contents
                                        string strCheck = "";
                                        string strReqdAtts = "";
                                        for (int i = 0; i < arrConference.Length - 1; i++)
                                        {
                                            strCheck = strCheck + arrConference[i] + "\n";
                                            strReqdAtts = strReqdAtts + ";" + arrConference[i];
                                        }

                                        //MessageBox.Show("Array contents\n\n" + strCheck);
                                        strReqdAtts = strReqdAtts.TrimStart(';');
                                        //MessageBox.Show("RequiredAttendees contents\n\n" + strReqdAtts);
                                        meeting.RequiredAttendees = strReqdAtts;
                                        //MessageBox.Show(".Add");
                                    }
                                    MessageBox.Show("Attendees assigned");


                                    Cancel = true;
                                    // has the user included "JHP - Room Reservations" for placement
                                    // on the shared calendar for all conference room availability
                                    foreach (Outlook.Recipient objUser in meeting.Recipients)
                                    {
                                        if (objUser.Name == "JHP - Room Reservations")
                                            blnTorF = true;
                                    }
                                    // add "JHP - Room Reservations" if not already included
                                    if (blnTorF == false)
                                    {
                                        recipConf = meeting.Recipients.Add("JHP - Room Reservations");
                                        recipConf.Type = (int)Outlook.OlMeetingRecipientType.olRequired;
                                    }
                                    // resolve recipients
                                    meeting.Recipients.ResolveAll();
                                    meeting.Send();
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Calendar recipients were not added.");
                    }
                    finally
                    {
                        if (recipConf != null) Marshal.ReleaseComObject(recipConf);
                    }
                }
                else
                //{
                    // ItemSend is a cancel
                    MessageBox.Show("Meeting is cancelled.");
                //}
            }
            else
            {
                MessageBox.Show("Not a meeting");
            }
        }

        #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
    }
EN

回答 2

Stack Overflow用户

发布于 2022-10-21 21:31:25

AppointmentItem从不发送,因此也不会传递给Application.ItemSend事件。您得到的是MeetingItem对象。如果需要父约会,请调用MeetingItem.GetAssociatedAppointment

票数 0
EN

Stack Overflow用户

发布于 2022-10-21 21:32:22

触发Explorer事件时不需要使用NewInspector窗口:

代码语言:javascript
运行
复制
 void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
        {
            // <testing this>
            Outlook.Selection sel = Application.ActiveExplorer().Selection;

使用作为参数传递给事件处理程序的Inspector实例。

我还注意到ItemSend事件处理程序中的以下代码:

代码语言:javascript
运行
复制
 public void Application_ItemSend(object Item, ref bool Cancel)
        {
            // use EventId to identify current meeting request
            var app = new Microsoft.Office.Interop.Outlook.Application();
            var ns = app.Session;

每次触发事件时,都不需要创建新的Outlook Application实例。改为使用外接程序类的Application属性(在ThisAddIn类中可用)。

此外,您还需要使用作为参数传递给Item事件处理程序的ItemSend对象,所以不要使用以下代码:

代码语言:javascript
运行
复制
Outlook.AppointmentItem meeting = ns.GetItemFromID(objUserPropertyEventId.Value);
            MessageBox.Show(meeting.Subject);

检查对象是否属于特定类型:

代码语言:javascript
运行
复制
if (Item is Outlook.MeetingItem)

您可以尝试将Item对象强制转换为特定类型,然后检查是否为null

代码语言:javascript
运行
复制
var obj = Item as Outlook.MeetingItem;
if (obj != null)
{
  'do your work here
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74158865

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档