Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use dll's in the same directory as an excel file

Tags:

windows

excel

dll

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.

like image 736
Rook Avatar asked Jun 09 '09 16:06

Rook


People also ask

How do you call a DLL in Excel VBA?

You can access DLL functions and commands in VBA by using the Declare statement. This statement has one syntax for commands and one for functions. The optional Public and Private keywords specify the scope of the imported function: the entire Visual Basic project or just the Visual Basic module, respectively.

Where are DLLs stored?

Your DLL files are located in C:\Windows\System32. When Windows Defender runs a Full Scan, it includes that directory and so all of your DLLs will be scanned. This will scan your DLL files for any malware infections.

How do I register a DLL in Excel?

Excel workbook solution Press the Select DLL button, find the location of the DLL file on your hard disk and select it. After selecting the DLL file you have two choices: you can either register or unregister the DLL file by pressing the buttons Register DLL and Unregister DLL correspondingly.


2 Answers

The way I generally take care of this is by adding:

Dim CurrentPath As String
CurrentPath = CurDir()   
ChDir (ThisWorkbook.Path)

To: Private Sub Workbook_Open()

like image 36
Ricardo Affinito Avatar answered Oct 09 '22 02:10

Ricardo Affinito


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.

  1. Create a new module at runtime (you could import a .bas file from disk, no need to hard-code the module with string literals), using the VBIDE Extensibility API. Drawback: no compile-time validation; you'll need to use stringly-typed Application.Run calls to invoke it. Requires trusted programmatic access to the VBIDE API (i.e. you allow VBA to execute code that generates code that is then executed... like macro viruses do).
  2. Use the LoadLibrary Win32 API... and now you've got pointers and addresses: this scary code (.zip download) is essentially a huge unmaintainable hack that uses assembly language to enable invoking the API functions by name. Looks like it only works for a subset of supported Win32 API functions though.
  3. Change the DLL search path, but then that also requires dynamic code added at run-time, so might as well go with the above.

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!

like image 160
ewbi Avatar answered Oct 09 '22 04:10

ewbi