cancel
Showing results for 
Search instead for 
Did you mean: 

VBA and COM ....

david_fryda2
Participant
0 Kudos

Hi everyone,

I am a really new at trying to call COM from VBA.

Here is what I've done.

a) I creates a Class Library in C# with a really simple method.

b) I created a tlb and snk file using sn.exe, gacutil.exe, regasm.exe.

I checked that my class is located in \windir\assembly.

c) I opend my Outlook 2002 and created a macro.

d)In the Tools->References menu, I selected the tlb file.

e) here is the code of the VBA macro:

Sub Macro1()

Dim objAdd As New ClassLibrary1.Class1

End Sub

This compile.

But, when I try to know the methods in the objAdd object...there aren't.

This object should contain the Add method to add 2 numbers.

Can someone help me with this issue ?

Thanks a lot.

Accepted Solutions (1)

Accepted Solutions (1)

reiner_hille-doering
Active Contributor
0 Kudos

Here is a sample that works for me:

[Guid("D443AD23-0914-4e0f-8FA2-8AAA472F3882")]

[ClassInterface(ClassInterfaceType.AutoDual)]

[ProgId("Demo.Class1")]

[ComVisible(true)]

public class Class1

{

public int Add(int a, int b)

{

return a + b;

}

}

If you regasm with /codebase or regasm without it and put the assembly into GAC, it works fine from Excel.

david_fryda2
Participant
0 Kudos

Hi Reiner,

What are those parameters ? Can you give me a short explanation for them ?

By the way, I succeed calling the COM without those parameters.

Thanks for your help.

reiner_hille-doering
Active Contributor
0 Kudos

The stuff in the brackets are COM-Interop-Custom attributes (namespace System.Runtime.Interop).

You don't absolutely need them, but you can better control the behavior of your COM-Interop:

[Guid("D443AD23-0914-4e0f-8FA2-8AAA472F3882")]

Defines the CLSID of the COM-Class. If not specified, the CLSID is automatically calculated as a hash from full class name and full assembly name. The advantage of specifying it is that you easier find you stuff in the registry and it remains the same if you rename something.

[ClassInterface(ClassInterfaceType.AutoDual)]

Describes the type of COM interface for the class. For VBS you need either Dual or Dispatch.

[ProgId("Demo.Class1")]

The so-called ProgID of you COM class that appears in HKEY_CLASSES_ROOOT. If you don't specify, you will get a ProgID somehow derived from the class name.

[ComVisible(true)]

Turns COM interop for the class on. If you turn off ComVisible for the whole assembly, you need to turn it on again on class level.

david_fryda2
Participant
0 Kudos

Thanks Reiner for all the help you gave.

Answers (1)

Answers (1)

Former Member
0 Kudos

Well. . i´m think. that you can´t found the library.

for this check this link to follow the steps to install your library like a Component Service

http://www.easterndigitalsoftware.com/justldap/walkthrough.htm