?
Solved

Calling c# .net Dll in VBA compile error

Posted on 2012-09-18
4
Medium Priority
?
618 Views
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
{
    [ClassInterface(ClassInterfaceType.AutoDual)]
    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?

Thanks,
HeadPygmie
0
Comment
Question by:headpygmie
  • 2
  • 2
4 Comments
 
LVL 20

Expert Comment

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

http://edndoc.esri.com/arcobjects/9.2/NET/9826f81b-278c-49cc-a912-ab8d86f5e91e.htm
http://msdn.microsoft.com/en-us/library/tzat5yw6(v=vs.90).aspx

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

Author Comment

by:headpygmie
ID: 38411264
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.
0
 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 2000 total points
ID: 38411307
0
 

Author Comment

by:headpygmie
ID: 38411526
Thanks!!!  I set the c# type to int and that worked.  I guess I'll to be more careful in the future.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

840 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