Hi, I am trying to create a C# VSTO Add In for outlook 2016 connected to Exchange 2016 (I know it's old but I am tied to these versions for now) - starting with something simple.
required function:
user creates a meeting in outlook calendar and invites users plus a conference room resource
when email of conference resource is detected all recipients receive an email with prepopulated conference details
NB: code works with MailItem but not AppointmentItem
I tried creating a new install of windows 11, with office 2016 (no previous versions installed) and error persisted.
error:
System.InvalidCastException: 'Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.AppointmentItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063033-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).'
This is generated in debug on the line in bold below Outlook.AppointmentItem appointmentItem = (Outlook.AppointmentItem)item;
Code
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Windows.Forms;
namespace MyCMSAddIn
{
public partial class ThisAddIn
{
Outlook.AppointmentItem appointment;
Outlook.Inspectors inspectors;
void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
{
if (Inspector.CurrentItem is Outlook.AppointmentItem)
appointment = Inspector.CurrentItem as Outlook.AppointmentItem;
else
appointment = null;
}
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
// Get the Application object
Outlook.Application application = this.Application;
// Get the Inspectors objects
Outlook.Inspectors inspectors = application.Inspectors;
// Get the Explorers objects
Outlook.Explorers explorers = application.Explorers;
// Subscribe to the ItemSend event, that it's triggered when an email is sent
application.ItemSend +=
new Outlook.ApplicationEvents_11_ItemSendEventHandler(
ItemSend_BeforeSend);
inspectors = this.Application.Inspectors;
inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
((Outlook.ApplicationEvents_Event)this.Application).ItemSend += ItemSend_BeforeSend;
}
void ItemSend_BeforeSend(object item, ref bool cancel)
{
if (appointment != null)
{
Outlook.AppointmentItem appointmentItem = (Outlook.AppointmentItem)item;
Outlook.Recipients MyRecipients = appointmentItem.Recipients;
const string PR_SMTP_ADDRESS =
"http://schemas.microsoft.com/mapi/proptag/0x39FE001E";
Outlook.Recipients recips = appointmentItem.Recipients;
foreach (Outlook.Recipient recip in recips)
{
Outlook.PropertyAccessor pa = recip.PropertyAccessor;
string smtpAddress =
pa.GetProperty(PR_SMTP_ADDRESS).ToString();
if (smtpAddress == "(e-mail address removed)")
{
appointmentItem.Body += ("To Join the Conference");
appointmentItem.Body += ("Dial tel:881119");
appointmentItem.Body += ("or Click http://meeting.int.local");
appointmentItem.Body += (" ");
appointmentItem.Body += ("Please contact the Conference Organiser if you need to discuss this booking ");
}
cancel = false;
}
}
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
// Note: Outlook no longer raises this event. If you have code that
// must run when Outlook shuts down, see http://go.microsoft.com/fwlink/?LinkId=506785
}
#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(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}
required function:
user creates a meeting in outlook calendar and invites users plus a conference room resource
when email of conference resource is detected all recipients receive an email with prepopulated conference details
NB: code works with MailItem but not AppointmentItem
I tried creating a new install of windows 11, with office 2016 (no previous versions installed) and error persisted.
error:
System.InvalidCastException: 'Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.AppointmentItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063033-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).'
This is generated in debug on the line in bold below Outlook.AppointmentItem appointmentItem = (Outlook.AppointmentItem)item;
Code
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Windows.Forms;
namespace MyCMSAddIn
{
public partial class ThisAddIn
{
Outlook.AppointmentItem appointment;
Outlook.Inspectors inspectors;
void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
{
if (Inspector.CurrentItem is Outlook.AppointmentItem)
appointment = Inspector.CurrentItem as Outlook.AppointmentItem;
else
appointment = null;
}
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
// Get the Application object
Outlook.Application application = this.Application;
// Get the Inspectors objects
Outlook.Inspectors inspectors = application.Inspectors;
// Get the Explorers objects
Outlook.Explorers explorers = application.Explorers;
// Subscribe to the ItemSend event, that it's triggered when an email is sent
application.ItemSend +=
new Outlook.ApplicationEvents_11_ItemSendEventHandler(
ItemSend_BeforeSend);
inspectors = this.Application.Inspectors;
inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
((Outlook.ApplicationEvents_Event)this.Application).ItemSend += ItemSend_BeforeSend;
}
void ItemSend_BeforeSend(object item, ref bool cancel)
{
if (appointment != null)
{
Outlook.AppointmentItem appointmentItem = (Outlook.AppointmentItem)item;
Outlook.Recipients MyRecipients = appointmentItem.Recipients;
const string PR_SMTP_ADDRESS =
"http://schemas.microsoft.com/mapi/proptag/0x39FE001E";
Outlook.Recipients recips = appointmentItem.Recipients;
foreach (Outlook.Recipient recip in recips)
{
Outlook.PropertyAccessor pa = recip.PropertyAccessor;
string smtpAddress =
pa.GetProperty(PR_SMTP_ADDRESS).ToString();
if (smtpAddress == "(e-mail address removed)")
{
appointmentItem.Body += ("To Join the Conference");
appointmentItem.Body += ("Dial tel:881119");
appointmentItem.Body += ("or Click http://meeting.int.local");
appointmentItem.Body += (" ");
appointmentItem.Body += ("Please contact the Conference Organiser if you need to discuss this booking ");
}
cancel = false;
}
}
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
// Note: Outlook no longer raises this event. If you have code that
// must run when Outlook shuts down, see http://go.microsoft.com/fwlink/?LinkId=506785
}
#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(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}