Outlook VSTO non si attiva sull'evento NewMailEx?
Domanda
Ho creato un VSTO in C # che dovrebbe agganciare l'evento NewMailEx di Outlook 2007. Tuttavia, a volte non si attiva quando eseguo un invio / ricezione manuale o quando nella posta in arrivo è presente solo 1 posta non letta. Sembra quasi che venga attivato nella posta in arrivo PRIMA che il messaggio arrivi effettivamente.
Esiste un modo migliore per monitorare i nuovi messaggi ogni volta oltre a ItemAdd o NewMailEX utilizzando VSTO?
Soluzione
Il motivo è: " GC raccoglie l'oggetto .NET, che avvolge l'oggetto COM da Outlook) " ;. La soluzione contiene riferimenti a questo oggetto .NET. Il modo più semplice è:
// this is helper collection.
// there are all wrapper objects
// , which should not be collected by GC
private List<object> holdedObjects = new List<object>();
// hooks necesary events
void HookEvents() {
// finds button in commandbars
CommandBarButton btnSomeButton = FindCommandBarButton( "MyButton ");
// hooks "Click" event
btnSomeButton.Click += btnSomeButton_Click;
// add "btnSomeButton" object to collection and
// and prevent themfrom collecting by GC
holdedObjects.Add( btnSomeButton );
}
Puoi anche avere un campo speciale per questo (e altri) pulsante concreto (o altri oggetti), se lo desideri. Ma questa è la soluzione più comune.