Проблема:
Событие запускается несколько раз, а не только один раз. Например, когда я использую события Inspectors.NewInspector и CurrentExplorer.SelectionChange, они срабатывают в ситуациях, когда этого не должно быть, например, для встроенных ответов или для новых инспекторов, не связанных с ответом.
Вот моя текущая реализация:
Код: Выделить всё
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
outlookApp = this.Application;
inspectors = outlookApp.Inspectors;
inspectors.NewInspector += Inspectors_NewInspector;
// Hook to ActiveExplorer for inline responses
currentExplorer = outlookApp.ActiveExplorer();
if (currentExplorer != null)
{
currentExplorer.SelectionChange += CurrentExplorer_SelectionChange;
}
}
private void Inspectors_NewInspector(Outlook.Inspector inspector)
{
try
{
if (inspector.CurrentItem is Outlook.MailItem mailItem)
{
// Cast the inspector to the event interface
Outlook.InspectorEvents_10_Event inspectorEvents = (Outlook.InspectorEvents_10_Event)inspector;
// Subscribe to the Activate event
inspectorEvents.Activate += () => Inspector_Activate(inspector, inspectorEvents);
}
}
catch (Exception ex)
{
MessageBox.Show($"Error: {ex.Message}");
}
}
private void CurrentExplorer_SelectionChange()
{
try
{
// Check if there is an inline response
var inlineResponse = Globals.ThisAddIn.Application.ActiveExplorer().ActiveInlineResponse;
if (inlineResponse != null && inlineResponse is Outlook.MailItem mailItem)
{
if (IsReplyEmail(inlineResponse))
{
// Adding signature
}
}
}
catch (System.Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error: {ex.Message}");
}
}
- Определять, когда электронное письмо с ответом или сообщением «ответить всем» готово к написанию.
Немедленно добавьте подпись в электронное письмо. - Убедитесь, что это происходит только один раз для каждого состава электронного письма.
Подробнее здесь: https://stackoverflow.com/questions/793 ... sto-add-in