473,323 Members | 1,589 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

C# class library for COM interop

Hello,

I want to create a C# class library which I can use in an Excel VBA macro. I
created a C# class library and set the "Register for COM Interop" option in
Visual Studio to TRUE. Now in Excel I can use this library but the
intellisense does not show any properties or functions of the class. Do I
have to add any keywords or GUID to each public function and property in C#?
Do I have to load the library to the global assembly cache?
Does anyone know a link to a tutorial in which a C# class library is created
to be used in VBA?

Thanks

Nov 16 '05 #1
7 4678
Make sure your methods are decorated with the ComVisibleAttribute

[ComVisible(true)]
public .... SomeMethod(...)

Willy.

"Josef" <Jo***@discussions.microsoft.com> wrote in message
news:C2**********************************@microsof t.com...
Hello,

I want to create a C# class library which I can use in an Excel VBA macro.
I
created a C# class library and set the "Register for COM Interop" option
in
Visual Studio to TRUE. Now in Excel I can use this library but the
intellisense does not show any properties or functions of the class. Do I
have to add any keywords or GUID to each public function and property in
C#?
Do I have to load the library to the global assembly cache?
Does anyone know a link to a tutorial in which a C# class library is
created
to be used in VBA?

Thanks

Nov 16 '05 #2
[ComVisible(true)] doesn't has any effect. The only thing which helped is

[ClassInterface(ClassInterfaceType.AutoDual)]
public class MyClass :IMyInterface

but when using AutoDual my functions which uses "ref" parameters such as
public string GetUnitStrings(ref int nr)
create an error in VBA

Josef
"Willy Denoyette [MVP]" wrote:
Make sure your methods are decorated with the ComVisibleAttribute

[ComVisible(true)]
public .... SomeMethod(...)

Willy.

"Josef" <Jo***@discussions.microsoft.com> wrote in message
news:C2**********************************@microsof t.com...
Hello,

I want to create a C# class library which I can use in an Excel VBA macro.
I
created a C# class library and set the "Register for COM Interop" option
in
Visual Studio to TRUE. Now in Excel I can use this library but the
intellisense does not show any properties or functions of the class. Do I
have to add any keywords or GUID to each public function and property in
C#?
Do I have to load the library to the global assembly cache?
Does anyone know a link to a tutorial in which a C# class library is
created
to be used in VBA?

Thanks


Nov 16 '05 #3
Well, [ComVisible(true)] is the default, but I was just guessing you could
have set it to false.

The [ClassInterface(ClassInterfaceType.AutoDispatch)] is default, so no
typelib info is emitted when the typelib is generated so there is no
intellisense support, and the interface can only be used from late-bound
clients.
(ref int nr) should work, please post some more code (C# and VBA) and the
error message in VBA.

Willy.

"Josef" <Jo***@discussions.microsoft.com> wrote in message
news:C7**********************************@microsof t.com...
[ComVisible(true)] doesn't has any effect. The only thing which helped is

[ClassInterface(ClassInterfaceType.AutoDual)]
public class MyClass :IMyInterface

but when using AutoDual my functions which uses "ref" parameters such as
public string GetUnitStrings(ref int nr)
create an error in VBA

Josef
"Willy Denoyette [MVP]" wrote:
Make sure your methods are decorated with the ComVisibleAttribute

[ComVisible(true)]
public .... SomeMethod(...)

Willy.

"Josef" <Jo***@discussions.microsoft.com> wrote in message
news:C2**********************************@microsof t.com...
> Hello,
>
> I want to create a C# class library which I can use in an Excel VBA
> macro.
> I
> created a C# class library and set the "Register for COM Interop"
> option
> in
> Visual Studio to TRUE. Now in Excel I can use this library but the
> intellisense does not show any properties or functions of the class. Do
> I
> have to add any keywords or GUID to each public function and property
> in
> C#?
> Do I have to load the library to the global assembly cache?
> Does anyone know a link to a tutorial in which a C# class library is
> created
> to be used in VBA?
>
> Thanks
>


Nov 16 '05 #4
the c# code look like this:
----------------------
namespace KUnits
{
interface IKUnits
{
string GetLabel(ref int nrOfStrings);
}
[ClassInterface(ClassInterfaceType.AutoDual)] // ???
public class Units :IKUnits
{
public Units()
{
}
public string GetLabel(ref int nrOfStrings)
{
string[] arr = new string[a];
...
nrOfStrings = arr.Length;
return arr;
}
....
}
}
----------------------

VBA Code:
----------------------
Set Units As KUnits.Units
Dim str as String
Dim sizeOf as Integer
str = Units.GetLabel(sizeOf)
....
----------------------

VBA error message at "str = Units.GetLabel(sizeOf)":
"Argument type ByRef incompatible" (I hope this is the right translation to
english)

Thanks
Josef
"Willy Denoyette [MVP]" wrote:
Well, [ComVisible(true)] is the default, but I was just guessing you could
have set it to false.

The [ClassInterface(ClassInterfaceType.AutoDispatch)] is default, so no
typelib info is emitted when the typelib is generated so there is no
intellisense support, and the interface can only be used from late-bound
clients.
(ref int nr) should work, please post some more code (C# and VBA) and the
error message in VBA.

Willy.

"Josef" <Jo***@discussions.microsoft.com> wrote in message
news:C7**********************************@microsof t.com...
[ComVisible(true)] doesn't has any effect. The only thing which helped is

[ClassInterface(ClassInterfaceType.AutoDual)]
public class MyClass :IMyInterface

but when using AutoDual my functions which uses "ref" parameters such as
public string GetUnitStrings(ref int nr)
create an error in VBA

Josef
"Willy Denoyette [MVP]" wrote:
Make sure your methods are decorated with the ComVisibleAttribute

[ComVisible(true)]
public .... SomeMethod(...)

Willy.

"Josef" <Jo***@discussions.microsoft.com> wrote in message
news:C2**********************************@microsof t.com...
> Hello,
>
> I want to create a C# class library which I can use in an Excel VBA
> macro.
> I
> created a C# class library and set the "Register for COM Interop"
> option
> in
> Visual Studio to TRUE. Now in Excel I can use this library but the
> intellisense does not show any properties or functions of the class. Do
> I
> have to add any keywords or GUID to each public function and property
> in
> C#?
> Do I have to load the library to the global assembly cache?
> Does anyone know a link to a tutorial in which a C# class library is
> created
> to be used in VBA?
>
> Thanks
>


Nov 16 '05 #5

"Josef" <Jo***@discussions.microsoft.com> wrote in message
news:8E**********************************@microsof t.com...
the c# code look like this:
---------------------- public string GetLabel(ref int nrOfStrings)
{
string[] arr = new string[a];
...
nrOfStrings = arr.Length;
return arr;
}
...


This method returns a string array while the method signature specifies a
string as return type, this can't compile.
Please submit the correct code.

Willy.
Nov 16 '05 #6

"Josef" <Jo***@discussions.microsoft.com> wrote in message
news:8E**********************************@microsof t.com...
the c# code look like this: public string GetLabel(ref int nrOfStrings)
VBA Code:
----------------------
Set Units As KUnits.Units
Dim str as String
Dim sizeOf as Integer
str = Units.GetLabel(sizeOf)
...
----------------------

VBA error message at "str = Units.GetLabel(sizeOf)":
"Argument type ByRef incompatible" (I hope this is the right translation
to
english)


Dim sizeOf as Long

int in .NET are 32bit entities, Integer in VBA are 16bit, Long is 32 bit.
Willy.

Nov 16 '05 #7
Sorry,

string[] GetUnitStrings(string unitGroup, bool useUnicode, ref int
nrOfStrings)
should be the right methode.

Now I resolved the problem. When I use
"Dim sizeOf as Long" in VBA everything works fine

But:
1. Is it the right way using
"[ClassInterface(ClassInterfaceType.AutoDual)]"? I read something that
someone should avoid using "ClassInterfaceType.AutoDual".
2. Do I have to define interfaces as I have done?
3. What do I have to do when I want to use the class library on another
computer (registration, gac, ...)?

Thanks
Josef

"Willy Denoyette [MVP]" wrote:

"Josef" <Jo***@discussions.microsoft.com> wrote in message
news:8E**********************************@microsof t.com...
the c# code look like this:

public string GetLabel(ref int nrOfStrings)


VBA Code:
----------------------
Set Units As KUnits.Units
Dim str as String
Dim sizeOf as Integer
str = Units.GetLabel(sizeOf)
...
----------------------

VBA error message at "str = Units.GetLabel(sizeOf)":
"Argument type ByRef incompatible" (I hope this is the right translation
to
english)


Dim sizeOf as Long

int in .NET are 32bit entities, Integer in VBA are 16bit, Long is 32 bit.
Willy.

Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Patrick | last post by:
I am almost certain that I could use HTTP Post/Get to submit XML Web Service call (over SSL as well, if using Version 3 of MSXML2) from an ASP Application? However, would I only be able to call...
6
by: Patrick | last post by:
Following earlier discussions about invoking a .NET class library via ..NET-COM Interop (using regasm /tlb) at...
6
by: Przemo | last post by:
Hi, Do you know some good RS232C class? There is one in VB.NET 101 Examples, but I think it is poor. 1. I can't for e.g. read into my application all data received. I must tell how many...
2
by: pieter.breed | last post by:
Hi All, The company I work for has traditionally used COM/ActiveX for the solutions that it provides. We are in the process of moving to .NET and a few applications have been written in VB.NET...
2
by: Brian Reed | last post by:
I have three projects in the same solution that are all class library projects. The projects are COM interop objects that will be used within my COM framework. It is important to me to have all...
1
by: Brian Henry | last post by:
does anyone have a class to interact with a com or lpt port? I need to be able to send hex codes through the data lines D1 - D8 on a LPT port... or D1-D4 on serial... i have seen classes in the...
13
by: Alek Davis | last post by:
Hi, Is it possible to access intrinsic ASP objects, such as Request, from a .NET class. Say, I have a .NET library exposed via a COM or COM+ wrapper. Can this library retrieve the request info...
1
by: Jim | last post by:
Have fully operational software package developed on VB.NET that worked until Jan 1 2003, with early stage deployments on Oct 10, Oct 23, Nov 11, Dec 12 and Dec 30. When attempted final...
4
by: Greg | last post by:
I have a very simple Visual Basic .NET 2003 class library project ClassLibrary1 configured to run an external program (in this case C:\WINDOWS\system32\wscript.exe), in the Debugging Configuration...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.