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?

Outlook
Outlook
A family of Microsoft email and calendar products.
4,181 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
4,078 questions
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.