How can I get the email item I am editing when replying to an email in the current window in VSTO Outlook add-in?

Jeff Lee 0 Reputation points
2023-11-10T02:47:21.38+00:00

I am trying to get the email item currently being edited when I reply to an email in the current window of a VSTO Outlook add-in. I am using the following code to get the item and add an attachment, but when I reply in the current window, the Globals.ThisAddIn.Application.ActiveInspector() returns null:

private void AddAttachment(IEnumerable<string> filePathList)         
{             
	var inspector = Globals.ThisAddIn.Application.ActiveInspector();                        	
    if (inspector != null && inspector.CurrentItem is Outlook.MailItem mailItem)             
    {            
	   foreach (var filePath in filePathList)              
       {                     
		    //add attachment to current mail        
       	    mailItem.Attachments.Add(filePath);            
       }
    }         
}

How can I modify the code to successfully get the email item I am editing when replying to an email in the current window?

Microsoft 365 and Office | Development | Other
Outlook | Windows | Classic Outlook for Windows | For business
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.