By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,607 Members | 1,978 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,607 IT Pros & Developers. It's quick & easy.

Calling C#.net .dll from VBA

P: n/a
Hello all:

I was wondering if anyone has tried (and got to work) calling a c#.net
..dll from VBA code?

I have been working on this for about 2 weeks and am not getting any
where.

The_King

Nov 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
I saw this in one of the .NET newsgroups just in case you haven't
checked there yet.

Bill E.

_________________
All 2 messages in topic - view as tree
Gary Clifford Aug 20 2004, 3:00 am show options

Newsgroups: microsoft.public.dotnet.csharp.general
From: g...@cityfusion.co.uk (Gary Clifford) - Find messages by this
author
Date: 20 Aug 2004 03:00:40 -0700
Local: Fri, Aug 20 2004 3:00 am
Subject: Calling a C# dll with VBA
Reply to Author | Forward | Print | Individual Message | Show original
| Report Abuse

Hi, can anyone help please.
I am trying to create a DLL in C# and then call it from Excel VBA. I
have plenty of experience with VBA but none with C#. I get the error
message "Can't find DLL entry point myFirstDll in testdll".
The code for this trivial example is in file testdll.cs as follows:
Class testdll {
Public static int myFirstDll () {
Return 5;
}

}
I compiled it using the command line as follows:

csc /t:library /out:testdll.dll testdll.cs
and called it with:
Declare Function myFirstDll Lib "testdll.dll" () as integer
Sub CallTestDll()
Msgbox myFirstDll()
End sub
Can anyone give me a pointer as to how I can get this to work.
Thanks.

Samuel Hon Aug 24 2004, 2:37 am show options

Newsgroups: microsoft.public.dotnet.csharp.general
From: nore...@samuelhon.co.uk (Samuel Hon) - Find messages by this
author
Date: 24 Aug 2004 02:37:16 -0700
Local: Tues, Aug 24 2004 2:37 am
Subject: Re: Calling a C# dll with VBA
Reply to Author | Forward | Print | Individual Message | Show original
| Report Abuse

I think the problem is the compilation.
There is a setting in Visual Studio "register for com interop" under
Project Properties / Build
I dont think .NET dll's are COM compliant without this flag
Sam

Nov 13 '05 #2

P: n/a
Wow, thanks, I missed that over there.

I will try this out.

The_King

Nov 13 '05 #3

P: n/a
If you have vs2003 or vs2005 you can create your class library project
as "testdll"

using System;
public class firstDll
{
public int n = 5;
public int myFirstDll()
{
return 6;
}
}

Before building the dll, make sure you selecc

Register for Com Interop True

in the Project properties window. Then in Access you make a reference
to the testdll.tlb object library you just created. To invoke the dll
do this in Access:

Sub testOutDll()
Dim f As testdll.firstDll
Set f = New testdll.firstDll
Debug.Print f.n
Debug.Print f.myFirstDll
End Sub

This will print 5 and 6 respectively in the debug window in Access.

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.