Solved

Calling dll from vba

Posted on 2000-04-03
9
835 Views
Last Modified: 2012-06-21
I have a spreadsheet which calls function from dll.Dll is registered by
addin.What I need is to call this function from VBA code.How do I write
declare statement for named ranges that this dll function accepts?I want to use "Declare" not "Run" worksheet function.
0
Comment
Question by:mdanny
9 Comments
 
LVL 13

Expert Comment

by:cri
ID: 2682644
Can not find the article for Office 97 but should you have this, try the procedure as given for Office 2000:

http://msdn.microsoft.com/library/officedev/odeopg/deovraccessingfunctionsindll.htm
0
 
LVL 8

Expert Comment

by:stochastic
ID: 2682679
mdanny,
Here is an example I did as my first attempt with DLLs to be called from VBA.

My DLL was called MyVBAdll.dll. It contained a function called CircumF, taking one Double (radius) as param and returning a double value as answer.

The sub CircumferenceTable is given just to show how the call to CircumF was made.

Hope this helps :-)
- stochastic

Declare Function CircumF Lib "D:\dlltrial\MyVBAdll.dll" _
    (ByVal radius As Double) As Double
   
Sub CircumferenceTable()
    Dim i As Double
   
    Worksheets(1).Activate
    Range("a1:b11").Clear
    Cells(1, 1) = "radius"
    Cells(1, 2) = "circumference"
    For i = 1 To 10
        Cells(i + 1, 1) = i
        Cells(i + 1, 2) = CircumF(i)
    Next
    Columns("a:b").AutoFit
End Sub

0
 
LVL 1

Author Comment

by:mdanny
ID: 2683036
I need to pass arrays(ranges) to func and back.
0
 
LVL 8

Expert Comment

by:stochastic
ID: 2683074
If I understood you correctly, your function inside DLL expects an array as a parameter. But you want to pass as a parameter, a range in place of an array.

Is this correct?

I do not know how to do that, or whether this is possible. It is unlikely that you can pass named ranges directly as parameters to a DLL function. But I think the following should work:

1. Write a function (in VBA) that will accept a range as a parameter.

2. Inside this function, declare an array and load it with the contents of the range, using VBA statements.

3. Once you have got the array, call the function inside the DLL with this array. [I assume you know how to do this].

4. When the DLL function returns, the array will have been modified if appropriate (unless you passed it BYREF).

5. Dump the contents of that array into your source range, if that is what you need.

Am I on the right track? I apologize if I completely misunderstood.

If you need examples of how to write a VC++ project that will generate a DLL functions callable from VBA, there are samples available, including those involving array parameters.

- stochastic
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 1

Author Comment

by:mdanny
ID: 2683194
I need to pass arrays(ranges) to func and back.
0
 
LVL 1

Author Comment

by:mdanny
ID: 2683222
The problem is I haven't wrote the dll so I can't change it's input parameters.
When I use it from spreadsheet I pass namws of ranges.The register looks like
=REGISTER("dllname.dll","thisfunc","JKRP",etc)
0
 
LVL 8

Expert Comment

by:stochastic
ID: 2683224
mdanny,

>I need to pass arrays(ranges) to func and back.

Yes, I saw this comment the last time, and I thought I was responding to that! Is something amiss here? Can someone else help clarify?

- stochastic
0
 
LVL 1

Author Comment

by:mdanny
ID: 2697708
Adjusted points from 50 to 58
0
 
LVL 1

Accepted Solution

by:
kuk010998 earned 58 total points
ID: 2705438
The third par to REGISTER is important here. Got to the REGISTER dox to check it out. Excel addin DLLs can use Excel-specific types, you cannot (directly) Declare equivalents in VBA. You could always hack, with CopyMemory and so on, but first you gotta understand what the DLL wants, and "JKRP" is a description to that.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: dandraka
There are several quick shortcuts that can make your life easier in Microsoft Programs.  These simple tips and tricks will your work more productive and you faster at completing your tasks! MS Word (1) Creating Re-usable Scraps You can create s…
PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
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…

759 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

26 Experts available now in Live!

Get 1:1 Help Now