Microsoft Office 2011 for Mac now allows VBA calls to external dynamic libraries. Workbooks with these custom functions can be saved as XLAM files and made available to all workbooks. I have searched the forums as well as the net at large and learned a lot, but some sample code would help tremendously for me. I have a lot of existing Excel VBA code calling DLL's on Windows and I want to port these to Mac Excel. Even code that works in MS Visual Studio and Intel Fortran does not work with strings on the Mac side as it appears it should with minor changes. I can get functions to work passing doubles and singles. Here is some example code:
VBA code:
Option Explicit
Option Base 1
Private Declare Sub mySub Lib "myLib.dylib" (ByVal myString As String, StringLength As Long) 'strings usually are passed a hidden 2nd string length
Function myFortranString()
Dim str As String * 8
Call mySub(str, Len(str)) ' also seen as mySub(str, 8&) - which I also have no success with
myFortranString = str
End Function
Fortran code:
subroutine mySub (str)
!DEC$ ATTRIBUTES C, DECORATE, ALIAS:'mySub' ::mySub
c !DEC$ ATTRIBUTES REFERENCE :: str
character str*8
str='success!'
return
end
Compile the fortran code: ifort -dynamiclib -m32 mySub.for and rename myLib.dylib (how do I make it name it in the commandline?) Place myLib.dylib in ~/lib folder (create it in your home directory). This location is automatically searched by Excel.
The attributes declaration makes the subroutine pass by value (like C), name capitalization (like C) and gets rid of the trailing underscore fortran adds to the library subroutine name. I thought this should make the VBA work as written. What am I missing?
Interestingly, I found that adding the 2nd declaration, which tells fortran it is not getting a string length, works as long as the VBA call does not pass the string length. However, I would have to change a LOT of existing VBA code to make that work.
Is there a "best" way to make these kind of dynamic libraries? Are there Xcode templates anywhere? Example code anywhere? Thanks for any help.
Jim