473,542 Members | 2,045 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4689
Make sure your methods are decorated with the ComVisibleAttri bute

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

Willy.

"Josef" <Jo***@discussi ons.microsoft.c om> wrote in message
news:C2******** *************** ***********@mic rosoft.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( ClassInterfaceT ype.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 ComVisibleAttri bute

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

Willy.

"Josef" <Jo***@discussi ons.microsoft.c om> wrote in message
news:C2******** *************** ***********@mic rosoft.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( ClassInterfaceT ype.AutoDispatc h)] 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***@discussi ons.microsoft.c om> wrote in message
news:C7******** *************** ***********@mic rosoft.com...
[ComVisible(true )] doesn't has any effect. The only thing which helped is

[ClassInterface( ClassInterfaceT ype.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 ComVisibleAttri bute

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

Willy.

"Josef" <Jo***@discussi ons.microsoft.c om> wrote in message
news:C2******** *************** ***********@mic rosoft.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( ClassInterfaceT ype.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( ClassInterfaceT ype.AutoDispatc h)] 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***@discussi ons.microsoft.c om> wrote in message
news:C7******** *************** ***********@mic rosoft.com...
[ComVisible(true )] doesn't has any effect. The only thing which helped is

[ClassInterface( ClassInterfaceT ype.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 ComVisibleAttri bute

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

Willy.

"Josef" <Jo***@discussi ons.microsoft.c om> wrote in message
news:C2******** *************** ***********@mic rosoft.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***@discussi ons.microsoft.c om> wrote in message
news:8E******** *************** ***********@mic rosoft.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***@discussi ons.microsoft.c om> wrote in message
news:8E******** *************** ***********@mic rosoft.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( ClassInterfaceT ype.AutoDual)]"? I read something that
someone should avoid using "ClassInterface Type.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***@discussi ons.microsoft.c om> wrote in message
news:8E******** *************** ***********@mic rosoft.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
14763
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 web-service in a an asynchronous mode (with a callback function)? If so, how?
6
6170
by: Patrick | last post by:
Following earlier discussions about invoking a .NET class library via ..NET-COM Interop (using regasm /tlb) at http://groups.google.com/groups?hl=en&lr=&threadm=%23Van7eSrEHA.4004%40TK2MSFTNGP10.phx.gbl&rnum=1&prev=/groups%3Fhl%3Den%26lr%3D%26selm%3D%2523Van7eSrEHA.4004%2540TK2MSFTNGP10.phx.gbl I have concluded that my .NET class library...
6
7627
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 bytes I would like to read. If I specify too many exception occures, if too less there are still some data in buffer.
2
6447
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 up to this point. Some of the applications are class libraries which need to be callable via the COM interface from some of the older applications....
2
2272
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 three projects building to the bin directory of my COM framework so that I don't have to use the /codebase option when registering the objects. ...
1
1753
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 past for VB.NET or C#.net, if you know of any or a way of interacting since whidbey is still unavaiblable yet (which has a system.io.port class built...
13
3251
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 (basically, server variables exposed via the Request object), when it is invoked from a traditional ASP (not ASP.NET) application? Any ideas? Thanks...
1
7083
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 deployment on Jan 2, 2004 got following messages and errors... I reset everything and tried several times agian. Then erased the setup project and tried to...
4
2339
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 Properties Dialog Box with command line arguments C:\test.vbs. When I run the project, I get an "unable to start debugging" error "unable to start...
0
7392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7571
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7670
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5867
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5246
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1798
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
943
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.