How to Create Excel Automation Add-in Using C#?

G

GordonC

I am trying to create an automation Add-in for Excel using C#. I have tried
to following the directions in the MSDN article
http://msdn2.microsoft.com/en-us/library/ms173189(vs.80).aspx

I can not make that simple example work. I have Office 2003 and VS .NET
2005. Also, I have noticed some errors in the written instructions.

I copied the code from the MSDN article and followed the directions.
1. Create a new Visual C# Class Library project called ExcelAddIn. ( I
assume that means a Windows>Class Library.)
2. I checked the box "Register for COM Interop" under Output>Build in the
Project Properties Window.
3. I built the project. I received a warning message.
"...ExcelAddIn.dll does not containg any types that can be registered for COM
Interop."
4. The MSDN article states to press F5 next to compile the project. This
must be an error since one can not execute a class library project.
5. The ExcelAddIn does not appear in the Excel Automation Servers Dialog Box.

How can I make this simple example work?

Does anyone have the complete code and project file for this example?

Thanks,

Gordon
 
G

GordonC

I have included the code for the sample ExcelAddIn project.

using System;
using System.Collections.Generic;
using System.Text;

using System.Runtime.InteropServices;

namespace TaxTables
{
[ClassInterface(ClassInterfaceType.AutoDual)]
public class TaxTables
{
public static double Tax(double income)
{
if (income > 0 && income <= 7000) { return (.10 * income); }
if (income > 7000 && income <= 28400) { return 700.00 + (.15 *
(income - 7000)); }
if (income > 28400 && income <= 68800) { return 3910.00 + (.25 *
(income - 28400)); }
if (income > 68800 && income <= 143500) { return 14010.00 + (.28
* (income - 68800)); }
if (income > 143500 && income <= 311950) { return 34926.00 +
(.33 * (income - 143500)); }
if (income > 311950) { return 90514.50 + (.35 * (income -
311950)); }
return 0;
}

[ComRegisterFunctionAttribute]
public static void RegisterFunction(System.Type t)
{
Microsoft.Win32.Registry.ClassesRoot.CreateSubKey
("CLSID\\{" + t.GUID.ToString().ToUpper() +
"}\\Programmable");
}

[ComUnregisterFunctionAttribute]
public static void UnregisterFunction(System.Type t)
{
Microsoft.Win32.Registry.ClassesRoot.DeleteSubKey
("CLSID\\{" + t.GUID.ToString().ToUpper() +
"}\\Programmable");
}
}
}

The warning I receive follows.

Warning 1 "C:\Documents and Settings\Gordon\My Documents\Visual Studio
2005\Projects\ExcelAddIn\ExcelAddIn\bin\Debug\ExcelAddIn.dll" does not
contain any types that can be unregistered for COM Interop. ExcelAddIn
Warning 2 "C:\Documents and Settings\Gordon\My Documents\Visual Studio
2005\Projects\ExcelAddIn\ExcelAddIn\bin\Debug\ExcelAddIn.dll" does not
contain any types that can be registered for COM Interop. ExcelAddIn
 
O

Otavio

Hi Gordon!

I try to make the same thing as you. For now, it works in my computer, but I
cannot install in anothers...

First, change the line
[ClassInterface(ClassInterfaceType.AutoDual)]
into
[ClassInterface(ClassInterfaceType.AutoDual), ComVisible(true)]

After you go into the file AssemblyInfo.cs in solutions explorer
and you change the line
[assembly: ComVisible(false)]
into
[assembly: ComVisible(true)]
Have you installed:
- the VSTO (Visual Studio Tools for Office)?
- the patch O2003PIA?
- .NET Framework 2.0?

If Excel doesn't find the add-in, perhaps Excel uses .Net 1.1 and your
add-in uses .NET 2.0
You have to install the patch office2003-KB907417-FullFile-ENU to fix that.
 
G

GordonC

Otavio:

Thanks for the suggestions.

I tried to add the ComVisible(true) attribute. It did not help.

I do not have VSTO 2005. I have VSTO 2003, but it is not installed.

I have the Office 2003 PIA installed for Excel. I can not find an update
for the PIA.

I do have .NET Framework 2.0 since I have Visual Studio .NET 2005 Standard.

I checked KB907417, and I have the version of Otkloadr.dll it would install.

I have decided to use the Extensibility>Shared Add-in template for the
project. Now the add-in is installed in Excel; however, Excel gives me a
message that it can not find mscoree.dll for the add-in. The MSDN
ms173189(v80.) article just specifies a class library project

Did you use the Extensibility>Shared Add-in template for your project?
 
O

Otavio

No, I don't use it. Unfortunantely, I don't know how to help you in this
case. I will search for information. If I find it, I will write you.

GordonC said:
Otavio:

Thanks for the suggestions.

I tried to add the ComVisible(true) attribute. It did not help.

I do not have VSTO 2005. I have VSTO 2003, but it is not installed.

I have the Office 2003 PIA installed for Excel. I can not find an update
for the PIA.

I do have .NET Framework 2.0 since I have Visual Studio .NET 2005 Standard.

I checked KB907417, and I have the version of Otkloadr.dll it would install.

I have decided to use the Extensibility>Shared Add-in template for the
project. Now the add-in is installed in Excel; however, Excel gives me a
message that it can not find mscoree.dll for the add-in. The MSDN
ms173189(v80.) article just specifies a class library project

Did you use the Extensibility>Shared Add-in template for your project?
--
Gordon Clark


Otavio said:
Hi Gordon!

I try to make the same thing as you. For now, it works in my computer, but I
cannot install in anothers...

First, change the line
[ClassInterface(ClassInterfaceType.AutoDual)]
into
[ClassInterface(ClassInterfaceType.AutoDual), ComVisible(true)]

After you go into the file AssemblyInfo.cs in solutions explorer
and you change the line
[assembly: ComVisible(false)]
into
[assembly: ComVisible(true)]
Have you installed:
- the VSTO (Visual Studio Tools for Office)?
- the patch O2003PIA?
- .NET Framework 2.0?

If Excel doesn't find the add-in, perhaps Excel uses .Net 1.1 and your
add-in uses .NET 2.0
You have to install the patch office2003-KB907417-FullFile-ENU to fix that.
 

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