Results 1 to 5 of 5

Thread: Creating DLL's in Visual Basic Express (2010) for use in Excel - Help!

  1. #1
    Junior Member
    Join Date
    Aug 12
    Posts
    17

    Creating DLL's in Visual Basic Express (2010) for use in Excel - Help!

    Please be gentle - this is my first post and as will be obvious, I don't know what I'm doing! And apologies if this is a repost as my first attempt didn't seem to appear.

    I'm very new to .NET having downloaded VB Express (2010) just a few days ago. I'm running this under Windows XP and using Office 2007.

    I'm trying to create a DLL that I can call from Excel VBA programs as VBA is too slow for my calculations. So far I haven't succeeded in creating a DLL that I can using in Excel VBA.
    What I have managed to do is:
    1) Create a class in VB Express (2010), build it as a DLL and then use this class in another VB project. This worked smoothly - I just had to add a reference to the 2nd project which used the DLL
    2) I have also managed to create a DLL in VC++ Express (2010) and call this from Excel VBA. I never even had to add the DLL as a reference. I simply had a line of code in VBA
    Code:
    Declare Function timestwo Lib "D:\TestC++NET\TestCDLL\Debug\testCdll.dll" _
          Alias "_timestwo@8" (ByVal x As Double) As Double
    In this case, the DLL contained just a single a function, not a class.

    When I try and add the DLL I created in (1) as a reference to VBA, I get an error in the VBA IDE which says "Can't add a reference to the specified file." (This seems a little odd because I had no problem adding the DLL as a reference to another VB 2010 project.)

    I've done quite a bit of digging on the web and haven't found a solution. I do see references to the fact that I have to register the DLL. When I run Regsvr32 I get the error message "MyDLL.dll was loaded but the DLLServerRegister entry point was not found. This file can not be loaded."

    So....how do I go about creating a DLL that can actually be "seen" and used by VBA? I'd prefer not to have to register the DLL - but if I do have to, I can live with this.

    Any help, hints, tips or tricks would be gratefully appreciated!

  2. #2
    Junior Member
    Join Date
    Aug 12
    Posts
    17

    Re: Creating DLL's in Visual Basic Express (2010) for use in Excel - Help!

    Update:
    When looking at the Project Compile screen, I fields like Build output path and various compile options.
    However, I do NOT see a "Register For COM Interop". ( I also don't see a "Generate XML documentation file")
    MS's documentation mentions that "Register For COM Interop" setting is not available for Windows Application or Console Application projects - but mine is a Class Library.
    I also notice that both the Project "Application*" and "Compile*" tabs have an asterisk at the end of the name.
    Does this perhaps mean that the options I'm looking for are not available in the VB 2010 Express version?
    It still seems strange that I could get a simple C++ function to be accessible in VBA via DLL (also using the Express version), while with VB, I annot.

    Yours, increasingly baffled
    Hedgepiglet

  3. #3
    Junior Member
    Join Date
    Aug 12
    Posts
    17

    Re: Creating DLL's in Visual Basic Express (2010) for use in Excel - Help!

    [Delete double post]
    Last edited by Hedgepiglet; Aug 31st, 2012 at 12:10 AM.

  4. #4
    PowerPoster kevininstructor's Avatar
    Join Date
    Jun 08
    Location
    Oregon
    Posts
    5,007

    Re: Creating DLL's in Visual Basic Express (2010) for use in Excel - Help!

    The Interop Forms Toolkit 2.1 provides what you need to write VB.NET code that is callable VB6 projects and would guess the same for use in Excel. The main thing to understand is when writing this code that anything that gets pushed back to the caller must be in understandable. Example you cannot return a List(Of String) from a LINQ statement but can write a LINQ statement that returns a String array as shown below taken from a demo I did using this library.

    Code:
    Public Function LINQ_Demo() As String()
        Dim Names As String() = New String() {"Bob", "Mary", "Jane", "Bob", "Joe", "Bill"}
        Return (From Name In Names Select Name Distinct Order By Name).ToArray
    End Function
    With that said there is a good deal to know when working with the ToolKit, both VB.NET and what is acceptable in an Excel module.

    Below is the COM registration for a DLL built using the TookKit which is generated for you.
    Code:
    #Region "COM Registration"
    
        ' These  GUIDs provide the COM identity for this class 
        ' and its COM interfaces. If you change them, existing 
        ' clients will no longer be able to access the class.
    
        Public Const ClassId As String = "a19204e3-dd80-4c33-a035-f4764e25ab60"
        Public Const InterfaceId As String = "f52d1e57-0cd0-4420-86bb-55492a50e8f1"
        Public Const EventsId As String = "cf789067-f3f8-4165-b5f0-02dd2e849162"
    
        'These routines perform the additional COM registration needed by ActiveX controls
        <EditorBrowsable(EditorBrowsableState.Never)> _
        <ComRegisterFunction()> _
        Private Shared Sub Register(ByVal t As Type)
            ComRegistration.RegisterControl(t)
        End Sub
    
        <EditorBrowsable(EditorBrowsableState.Never)> _
        <ComUnregisterFunction()> _
        Private Shared Sub Unregister(ByVal t As Type)
            ComRegistration.UnregisterControl(t)
        End Sub
    
    #End Region
    I don't have this installed currently so I cannot assist if you go down this path.
    Also please note that much of the information in the link above discuss forms but as shown in my code blocks above they are in a project without forms. How this is done? Read the documentation and roll some code.

  5. #5
    Junior Member
    Join Date
    Aug 12
    Posts
    17

    Re: Creating DLL's in Visual Basic Express (2010) for use in Excel - Help!

    Thanks for the help Kevin. In the end the main problem seemed to be that VB Express (2010) does not register the DLL it creates and this has to be done manually using regasm and creating a .tlb file which can then add in as a reference in Excel VBA.

    I ran into a further problem in that I cannot find a way to pass multi-dimensional arrays from VBA to my DLL. (Single dimension arrays are OK) I suspect this may be rather difficult to overcome, as I've sen nothing indicating how to do it. It seems that VBA and VB might store their arrays in different ways.

    I'm largely interested in just performing calcs in VB and I also found that VB didn't give me quite as much of a speed improvement as I'd hoped. Code that ran in 32 seconds in VBA took about 9 seconds in VB - clearly much faster but not the 5-10x improvement I was expecting.
    Unless I find a quick solution to the multi-dimensional arrays problem, I'll probably look for other solutions.

    Once again, thanks for the help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •