VBA using C# Struct from DLL file

Posted on 2011-10-28
Last Modified: 2013-12-16

I have created a simple C# Class that I am using in Excel & Access. The question I have is when I look in the object browser in either Access or Excel I can see the name of my class and its functions. I also have a struct defined in my class & in the object browser the stuct appears as its own class not as a member of 'MyClass' why is this the case? I have moved the struct decelartion out of 'MyClass' and put under the namespace but this does not seem to change anything.

namespace MyNameSpace

	class MyClass

		public struct MyStruct
			int VariableOne;
			int VariableTwo;

		int MyInt;
		public void MyFunctionOne()


		public void MyFunctionTwo()




Open in new window

Question by:mcs26
    LVL 40

    Accepted Solution

    It has to do with what Structure and Class means in VBA.

    Structures do not exist as such in VBA. There is something called a Type that looks like your C# structure, but contrary to what you can do in structure, a Type can contain only a set of variable. No method.

    In .NET, a structures can contain a constructor and methods. This is almost the equivalent to a VBA classes, that are very simple things. In order to be able to use all the features of a .NET structure, VBA must handle it as if is one of its own classes.

    Even if you do not have methods in your own structure, the mechanism that lets you see a structure in VBA is the same for any structure. It is mapped to a class instead of a Type, that would be a better match with your simple structure.

    Author Comment

    Hi James,

    Thanks for the reply and the explanation. So am I correct in saying that VBA will alway map a .Net Struct to a Class and that in VBA to use this 'class' you do not have to initalise it like you would for a normal class, i.e. below?

    Dim objClass as MyProject.MyClass
    set objClass = new MyProject.MyClass
    Dim objStruct as MyProject.Struct
    objStruct.VariableOne = 5

    Open in new window

    LVL 40

    Expert Comment

    by:Jacques Bourgeois (James Burger)
    I cannot be sure that it will be allways map it that way. I dropped VB6 and VBA quite fast after VB.NET came to be, so I did not have a lot of experience referencing .NET from VBA. I also do not use structures very often in my own classes.

    But I am quite sure it will. It did in the 2 or 3 instances where I had to do it back around 2002, while I was moving my code from VB classic to VB.NET.

    The explanations I gave you is not official, but I am also quite sure it is right. I would bet my 22 years experience in object programming, the 12 years I worked mostly in VB6 and VBA (still do a little VBA in Office) and my 12 years in .NET on the fact that the reasons I give are close to what really happens under the cover.

    A .NET structure having almost all the features of a VBA class (only the Terminate event is not there), and even a few extras (constructors with parameters, overloading of methods to name just two), it makes sense that the VBA environment sees it as a class.

    I can also ad that since the notion of a namespace did not exist in VBA, the concept of referencing a type declared inside of another type does not exist either in VBA, so the object browser and IntelliSence are not able to display a structure inside of a class.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now