Calling c# .net Dll in VBA compile error

Posted on 2012-09-18
Last Modified: 2012-09-18
Hello.  I created a simple c# .net DLL with just one function called "addTwoLongs".  When I try to call the routine in VBA, I get the following compile error:

Function or interface marked as restricted, or the function uses an Automation type not supported in Visual Basic.

Here is my code in c#:

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

namespace MyTestDLL
    public class MyClass
        public long addTwoLongs(long a, long b)
            return (a + b);

Here is my code in VBA (using Excel 2010):

Public Function AddTheNumbers(ByVal num1 As Long, ByVal num2 As Long) As Long

    Dim o As MyClass
    Set o = New MyClass
    AddTheNumbers = o.addTwoLongs(num1, num2)
End Function

As you can see, it's a fairly simple dll.

Any ideas how to fix this?

Question by:headpygmie
    LVL 19

    Expert Comment

    by:Daniel Van Der Werken
    The .NET assembly needs to be "findable" by the COM system. Are you registering the DLL after you build it with regasm?

    It might have to be signed. I can't recall because it's been a while since I've done interop.

    Author Comment

    Thanks for the reply.  Yep, it is findable.

    In VBA references, I've found and checked the DLL.

    In the c# assembly information for the project, I also set "Make assembly COM-Visible" to checked.

    In addition, I also checked "Register for COM interop" in the build options.

    I hope I didn't miss anything else.
    LVL 19

    Accepted Solution


    Author Comment

    Thanks!!!  I set the c# type to int and that worked.  I guess I'll to be more careful in the future.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
    Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
    The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
    The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now