c# excel dll - can't add a reference to the specified file - regasm

user1680643 picture user1680643 · Sep 18, 2012 · Viewed 8.7k times · Source

When deploying and registering a .Net Excel.dll on another computer, I get an error Can't add a reference to the specified file when attempting to add reference to DLL in VBA editor.

I have created the Excel.dll in C# in Visual Studio that runs fine on my machine with Windows 7 and Office 2010. No problem adding reference to the dll in Excel VBA editor on my computer. My problem is deploying on another machine which is running Vista and Excel 2007. I copied dll to this computer and used regasm to register the dll.

Can anyone point me in the right direction? Here is code and regasm:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm.exe excelDll.dll

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace TestDll
{
[ClassInterface(ClassInterfaceType.AutoDual)]
public class Test
{
    public string HelloWorld
    {
        get
        {
            return "Hello World";
        }
    }

    public void sayGoodbye1()
    {
        MessageBox.Show("Say Goodbye");

    }

    public string sayGoodbye2()
    {
        return "Say Goodbye";
    } 
  }
}

Answer

Mark Harrison picture Mark Harrison · Nov 10, 2012

You need to register the type library for excel to see your dll in References.

i.e. regasm.exe excelDll.dll /tlb:excelDll.tlb

Mark.