Rick Shuri

It appears that if a user is using WordEditor to compose an email message, then the recipients in the mail item are unavailable in the object model (unless the mail item is saved programmatically - resulting in unwanted entries in the user's Drafts folder.)

Is this possible Am I doing something wrong or do I have to write a kludge around this Most of my users use Word as their email editor.

Below is very simplied code that demonstrates the issue. It is a complete VSTO add-in for Outlook 2003. To prove this point, run the Add-In, create a new mail message and add an email recipient in the To... line. Then click the Add-in's "Click Me" menu item. You must be using Word to edit your email messages.

Any insight would be most appreciated.

--Rick


Using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using Word = Microsoft.Office.Interop.Word;

namespace OutlookAddin1
{
public partial class ThisApplication
{

Outlook.Inspectors _inspectors = null;
Outlook.Inspector _inspector = null;
private Office.CommandBar _menuBar = null;
private Office.CommandBarPopup _popup = null;
private Office.CommandBarButton _button = null;


private void ThisApplication_Startup(object sender, System.EventArgs e)
{
_inspectors = this.Inspectors;
_inspectors.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(_inspectors_NewInspector);
}

private void ThisApplication_Shutdown(object sender, System.EventArgs e)
{
}

void _inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
{
_inspector = Inspector;
((Outlook.InspectorEvents_10_Event)_inspector).Close += new Outlook.InspectorEvents_10_CloseEventHandler(InspectorCloseEvent);
AddMenu();
}

private void InspectorCloseEvent()
{
RemoveMenu();
((Outlook.InspectorEvents_10_Event)_inspector).Close -= new Outlook.InspectorEvents_10_CloseEventHandler(InspectorCloseEvent);
Marshal.ReleaseComObject(_inspector);
}

private void AddMenu()
{
_menuBar = _inspector.CommandBars.ActiveMenuBar;
_popup = (Office.CommandBarPopup)_menuBar.Controls.Add(Office.MsoControlType.msoControlPopup, missing, missing, _menuBar.Controls.Count + 1, true);
_popup.Caption = "My Add In";
_button = (Office.CommandBarButton)_popup.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing,1, true);
_button.Caption = "Click Me";
_button.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(_button_Click);
}

void _button_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
{
Outlook.MailItem mailItem = (Outlook.MailItem)_inspector.CurrentItem;
MessageBox.Show("The TO address line contains: " + mailItem.To);
}

private void RemoveMenu()
{
_button.Delete(true);
_popup.Delete(true);
Word.DocumentClass doc = (Word.DocumentClass)_inspector.WordEditor;
Word.Template tmpl = (Word.Template)doc.Application.CustomizationContext;
tmpl.Saved = true;

}

#region VSTO generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisApplication_Startup);
this.Shutdown += new System.EventHandler(ThisApplication_Shutdown);
}

#endregion
}
}



Re: Visual Studio Tools for Office Outlook Add-in - get Recipients from MailItem?

Sue Mosher - Outlook MVP

You may see that behavior regardless of the editor. Try calling MailItem.Recipients.ResolveAll or, as you've found, saving the item.

Perhaps the important issue is why does your add-in need to access those recipients at that point in the message creation process. If you explained that, maybe someone could suggest an alternative approach.





Re: Visual Studio Tools for Office Outlook Add-in - get Recipients from MailItem?

Rick Shuri

Thanks for your reply, Sue. Unfortunately, ResolveAll() does not provide a solution for this situation.

My add-in needs to resolve user-entered email addresses against a back-end database of 8000+ contacts to ensure accuracy, as well as providing a UI to acquire additional addresses from the db for that email. Ultimately, the add-in will be capturing the email in a CRM-type application and associating that email to one or more of those contacts.




Re: Visual Studio Tools for Office Outlook Add-in - get Recipients from MailItem?

Sue Mosher - Outlook MVP

One solution would be to provide your own UI for composing mail messages and resolving addresses and then use Outlook automation to create the actual outgoing message from the resulting data. That gets complicated if your add-in needs to support versions before Outlook 2007.

Another would be to expose the back-end database as a MAPI address book provider, so you can take advantage of Outlook's internal address resolution functionality.