tags:

views:

356

answers:

3

This is somewhat related to my other question.

I've been using a dll to acompany an excel spreadsheet. Everything is currently working with the dll and excel is using it just fine. But is it possible to specify that a dll resides in the same directory as the excel file when declaring functions?

Declare Sub FortranCall Lib "Fcall.dll" (r1 As Long, ByVal num As String)

Unfortunetly this doesn't work, I have to use something like:

Declare Sub FortranCall Lib "C:\temp\Fcall.dll" (r1 As Long, ByVal num As String)

This works, but is going to cause headaches when distributing to my office mates. Placing the dll in c:\windows\system32 etc. is not really an option either.

A: 

You can put the DLL in some directory and add it to the path EnVar.

Assaf Lavie
Please, could you explain a little more what did you mean. Did you mean I add the DLL into the PATH enviromental variable ? I'm afraid that will not be a satisfactory solution - it would be easier in that case to just ask them to keep the excel and dll file in a predefined directory scructure. I'm trying to accomplish as much as "portable" solution as possible.
ldigas
A: 

ActiveWorkbook.Path gives you the full path to the folder containing the currently-active workbook. So try this:

Declare Sub FortranCall Lib ActiveWorkbook.Path & "\Fcall.dll" (r1 As Long, ByVal num As String)
Ken Paul
Excel reports, "compile error, expected string constant" for ActiveWorkbook.Path ? Do you perhaps know what that means (I don't know VB much, so this may be something trivial ...)
ldigas
It means that for a Declare statement, you need a constant string - and ActiveWorkbook.Path is a function call (or a property), so you can't use it.
Ant
+1  A: 

Here are three possibilities for dynamically loading/calling into DLLs from VBA, including links to relevant info and some sample code. Can't say I've ever had to use any of the solutions described there, but it seems like a reasonable exploration of the options in light of VBA's need for a static path.

Here's another potential solution that suggests programmatically updating the PATH environment variable prior to calling into your DLL. Not a bad idea, if it works, as you could add this to you workbook open event.

Good luck!

ewbi