0
votes

When working with 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

But none of the below events working, Actually this line (myappt = Inspector.CurrentItem) get hit when open a new Appointment.

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

Actually whenever the Appointment Time change I want to capture that event and want to perform some action.

Is there I'm missing some event handler for Property change

1
Are you getting an error or is the event not firing? How did you add the events?Jen
Got the answer missing the Handler Event myappt = DirectCast(Inspector.CurrentItem, Outlook.AppointmentItem) AddHandler myappt.PropertyChange, AddressOf myappt_PropertyChangeMuthu.Krish

1 Answers

1
votes

The code is valid for VBA, but not for VB.NET where event handlers are not added automatically when you declare objects with events. For example, here is the code in 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

In VB.Net you need to use the AddHandler and RemoveHandler statements that allow you to start and stop event handling at any time during program execution. The Handles keyword and the AddHandler statement both allow you to specify that particular procedures handle particular events, but there are differences. The AddHandler statement connects procedures to events at run time. Use the Handles keyword when defining a procedure to specify that it handles a particular event. For more information, see Handles.

AddHandler appt.PropertyChange, AddressOf myappt_PropertyChange