【问题标题】:VSTO Outlook Appointment Event (VB.NET)VSTO Outlook 约会事件 (VB.NET)
【发布时间】:2020-07-23 19:23:02
【问题描述】:

使用 Outlook VSTO [VB.NET] VISUAL Studio 2019 时

Imports Microsoft.Office.Tools
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Outlook

Private WithEvents inspectors As Outlook.Inspectors
Private WithEvents myappt As Outlook.AppointmentItem
    
Private Sub ThisAddIn_Startup() Handles Me.Startup
     inspectors = Me.Application.Inspectors
End Sub
    
Private Sub inspectors_NewInspector(ByVal Inspector As Microsoft.Office.Interop.Outlook.Inspector) Handles inspectors.NewInspector
     If TypeOf Inspector.CurrentItem Is Outlook.AppointmentItem Or TypeOf Inspector.CurrentItem Is Outlook.MeetingItem Then
                myappt = Inspector.CurrentItem
     End If
End Sub

但以下事件均无效,实际上这一行 (myappt = Inspector.CurrentItem) 在打开新约会时会受到影响。

 Private Sub myappt_PropertyChange(ByVal Name As String)
        MsgBox(Name)
 End Sub
 Private Sub myappt_Close(Cancel As Boolean)
        MsgBox("Hi")
 End Sub

实际上,每当约会时间发生变化时,我都想捕获该事件并想执行一些操作。

我是否缺少一些属性更改的事件处理程序

【问题讨论】:

  • 您遇到错误还是事件未触发?您是如何添加事件的?
  • 得到答案缺少处理程序事件myappt = DirectCast(Inspector.CurrentItem, Outlook.AppointmentItem) AddHandler myappt.PropertyChange, AddressOf myappt_PropertyChange

标签: vb.net vsto outlook-addin


【解决方案1】:

代码对 VBA 有效,但对 VB.NET 无效,因为在声明带有事件的对象时不会自动添加事件处理程序。例如,这里是 VBA 中的代码:

Public WithEvents myItem As Outlook.AppointmentItem 

Sub Initialize_handler() 
 Set myItem = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderCalendar).Items("Status Meeting") 
End Sub 
 
Private Sub myItem_PropertyChange(ByVal Name As String)
 Select Case Name 
 Case "ReminderSet" 
 MsgBox "You may not remove a reminder on this item."
 myItem.ReminderSet = True 
 Case Else 
 End Select 
End Sub

在 VB.Net 中,您需要使用 AddHandlerRemoveHandler 语句,它们允许您在程序执行期间随时启动和停止事件处理。 Handles 关键字和AddHandler 语句都允许您指定特定过程处理特定事件,但有区别。 AddHandler 语句在运行时将过程连接到事件。定义过程时使用Handles 关键字来指定它处理特定事件。如需更多信息,请参阅Handles

AddHandler appt.PropertyChange, AddressOf myappt_PropertyChange

【讨论】:

    猜你喜欢
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    • 2018-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-15
    相关资源
    最近更新 更多