Peter,
In the first part of your question, a variable named "applicationObject" is
being cast as a reference to an Outlook application instance. If you are
referring to the familiar .NET Add-in sample using Outlook (the one that was
shown at the Office Launch or the one on MSDN), it was created using the
VS.NET Extensibility project (Shared add-in) which stubbed out the code to
connect Office to the VS project. In the OnConnection routine, it is passed
a generic Application object (as seen below), then to use it as an Outlook
app object instead... simply recast:
public void OnConnection(object application, Extensibility.ext_ConnectMode
connectMode, object addInInst, ref System.Array custom)
{
applicationObject = (Outlook.Application)application;
addInInstance = addInInst;
}
In the second part of your q, your code can work for each of the apps you
listed but since the Add-in is 'shared' you need to manage things
differently around the setting of the app object (differnent variables) to
refer to the specifics of each Office app. Here's the changes I made to run
Excel, Word, Outlook, and PowerPoint from the same code base:
//Added namespaces >>>>>>
using System.Reflection;
using System.Windows.Forms;
using Office = Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;
using Word = Microsoft.Office.Interop.Word;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Outlook = Microsoft.Office.Interop.Outlook;
//Added variables >>>>>>
public Word.Application wdApp;
public Excel.Application xlApp;
public Outlook.Application olApp;
public PowerPoint.Application pptApp;
//Changed the OnConnection routine >>>>>>
public void OnConnection(object application, Extensibility.ext_ConnectMode
connectMode, object addInInst, ref System.Array custom)
{
addInInstance = addInInst;
object oOffice = application.GetType().InvokeMember("Name",
BindingFlags.GetProperty,null,application,null);
string sName = oOffice.ToString();
switch (sName.ToLower())
{
case "microsoft word":
applicationObject = (Word.Application)application;
break;
case "microsoft excel":
applicationObject = (Excel.Application)application;
break;
case "microsoft powerpoint":
applicationObject = (PowerPoint.Application)application;
break;
case "microsoft outlook":
applicationObject = (Outlook.Application)application;
break;
default:
//blah
break;
}
MessageBox.Show(sName, "Office Addin");
}
Here's a good read on the low-down behind the madness
http://support.microsoft.com/?kbid=302901#3 plus a couple more:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnofftalk/html/office06062002.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnoxpta/html/odc_comshim.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnoxpta/html/odc_dnscof.asp