Consulting

Results 1 to 10 of 10

Thread: Calling DLL function from Excel VBA doesn't return value

  1. #1
    VBAX Newbie
    Joined
    Apr 2013
    Posts
    4
    Location

    Unhappy Calling DLL function from Excel VBA doesn't return value

    I decided to convert my VBA XLAM add-in to VB6 COM DLL and did so. Most functions work well except those that involve passing excel objects.

    As a simple example consider the following.

    [vba]
    Public Function dllReturnRowCount(rDataRange as Excel.Range) as Long
    dllReturnRowCount = rDataRange.Rows.Count
    End Function[/vba]

    The above is a simple function defined in VB6 COM DLL named say MyDLL in a class named say myDClass. The project is compiled into a DLL and a reference to it is added from within Excel VBA project. The below is the VBA wrapper to the above DLL function defined in Excel VBA.
    [vba]
    Function ReturnRowCount(rDataRange as Range) as Long
    Dim myDllFunc as MyDLL.myDClass
    Set myDllFunc = New MyDLL.myDClass
    ReturnRowCount = myDllFunc.dllReturnRowCount (rDataRange)
    End Function[/vba]

    When I call the above function and debug it from within VB6 and VBA both, I can see that the range object is passed to the DLL and the DLL function is able to operate on it and return the row count successfully (from within VB6 IDE). But once the function call returns back to VBA, the return value is empty and ReturnRowCount above returns zero even though dllReturnRowCount succeeded. I can get other functions that do not involve passing Excel objects to return values of types Date, String, Boolean etc. successfully.

    So, the question is how do I get the above to work the way it should ? What am I doing wrong ?

    Thanks for any help.
    Last edited by AcesHigh; 04-09-2013 at 10:15 AM.

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    How did you reference the Excel object?

    Here is a thread that might help some. It may not help fully if you are getting some functions to work that don't reference the Excel object. http://www.vbaexpress.com/forum/showthread.php?t=45316

  3. #3
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Also recently posted here
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  4. #4
    VBAX Newbie
    Joined
    Apr 2013
    Posts
    4
    Location
    How did you reference the Excel object?
    Err, you mean how did I refer to the Excel object in DLL ? Well, the first code block in my post is the DLL function that resides in the DLL and the second code block is the function declared in Excel VBA. Am I understanding you right here ?

  5. #5
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    To set the Excel object reference, see my link where I explained Imports or better yet, see Chip Pearson's example. http://www.cpearson.com/excel/creati...nctionlib.aspx

  6. #6
    VBAX Newbie
    Joined
    Apr 2013
    Posts
    4
    Location
    Thanks, I just checked out your link. Apparently, you are using .NET library and I am using VB6. So, there are differences in the way you would refer and access the Excel objects.

    What I have done is add a reference to the Microsoft Excel 12.0 Object Library from within VB6 References dialog as I have MS Office 2007 installed. Things are working well from the DLL end. The problem is when the DLL function call returns, it doesn't seem to pass the return value over to the VBA engine (when an Excel object is referenced in the DLL function).

    So, I am missing something here. May be it has to do with the way you access/refer the Excel objects or may be the problem is on the VBA end. Perhaps VB6 and Excel VBA 2007 are incompatible in some way that is causing this anomaly.

    So, I need to learn the proper way to address this in VB6 way.

    As an aside, if I use .NET and port the DLL to another machine would I need to have the .NET libraries installed on the other machine (I think yes, you would) and would it have to be the same version as the one used in the development of DLL ?

  7. #7
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Yes, the vb.net framework files need to be the same version or newer. This is the same principle as the old vbrunxxx.dll's only there are many more files. vb.net is a good way to go. Many computers already have basic framework files installed already.

  8. #8
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    how about trying
    [VBA]Function ReturnRowCount(rDataRange As Range) As Long
    Dim myDllFunc As Object
    Set myDllFunc = CreateObject( MyDLL.myDClass)
    Dim Result As Variant
    Result = myDllFunc.dllReturnRowCount (rDataRange)
    ReturnRowCount = Result 'or CLng(Result)
    End Function [/vba]
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  9. #9
    VBAX Newbie
    Joined
    Apr 2013
    Posts
    4
    Location
    how about trying
    VBA:
    Function ReturnRowCount(rDataRange As Range) As Long Dim myDllFunc As Object Set myDllFunc = CreateObject( MyDLL.myDClass) Dim Result As Variant Result = myDllFunc.dllReturnRowCount (rDataRange) ReturnRowCount = Result 'or CLng(Result) End Function
    Thanks. I just tried that, didn't work. VBA is throwing an error that says "Compile Error: method or data member not found" on myDClass on the below line:
    [VBA]Set myDllFunc = CreateObject ( myDLL.myDClass)[/VBA]

  10. #10
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    You need quotes
    [vba]Set myDllFunc = CreateObject("myDLL.myDClass")[/vba]
    Be as you wish to seem

Posting Permissions

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