Simple C# Office Add-in questions

P

Peter

Hello,

Thanks for reviewing my questions. In reference to
creating an Office Add-in, I have a couple simple
questions.

I have notice the following snip in Office Add-in samples
and would like to know what is being assigned - a
reference?

1) applicationObject = (Outlook.Application)application;

2) Is it a good idea to make the applicationObject a
static even though your add-in is for Excel, Word and
Outlook and all might be running at the same time or
should I make three separate static applicationObjects?

Many Thanks to the Experts.

Peter
 
C

Charles Maxson

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
 
P

Peter

Hi Charles,

Thanks for quick reply. I have a couple of follow up
questions:

1) applicationObject = (Outlook.Application)application;
is being assigned as a referenced. If so, shouldn't the
parameter be passed in as a reference?

OnConnection(object application,...
OnConnection(ref object application,...

2) Your code snip-it defines each of the Office
application objects but they never get assigned. Was
this just a mistake?

Thanks again
Peter
 
W

Wei-Dong Xu [MSFT]

Hi Peter,

Thank you for replying!

For your first question, from http://msdn.microsoft.com/library/d...ry/en-us/vbcn7/html/vaconparameterpassing.asp, ByVal is the
default convention in VB.NET projects. Specifying the ByVal keyword just makes the code easier to read.

Because the arguments being passed in are references to Word objects, it's important to protect them against being changed by the function. The
ByVal keyword ensures that the function cannot change the value of the arguments. ByRef would potentially allow the arguments to be modified.

For the second question, the application variable is passed into the procedure in the OnConnection event handler. The codes from Charles are very
smart which will detect the type of your office application and then cast the type to the corresponding type. This way, if you develop the add-in,
you will need to type the cast code each time.

Please feel free to let me know if you have any further questions.

Does this answer your question? Thank you for using Microsoft NewsGroup!

Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top