Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
The Intel sign-in experience is changing in February to support enhanced security controls. If you sign in, click here for more information.
27554 Discussions

Help with passing VBA string to Fortran

ripittard
New Contributor I
312 Views

I am trying to pass a VBA string to fortran, modify it and return the result and am having some trouble.  

 

I thought I could have VBA pass the size of the string and a reference to the string to Fortran and then use ConvertBSTRToString to obtain the Fortran equivalent, but I am struggling to do this correctly.

The lenght of the string comes across OK, but ConvertBSTRToString  result does not match and the string content in Fortran does not match.  Any help woudl be appreciated.

The Fortran routine and VBA code are attached.

 

Thanks,

 

 

0 Kudos
1 Solution
FortranFan
Honored Contributor II
293 Views

@ripittard ,

This post may be of limited value to you since what is below is prescriptive.  But if interested, take a look at this thread which has Fortran with VB.NET (a different beast of course) : https://community.intel.com/t5/Intel-Fortran-Compiler/x64-string-passing/td-p/1373313

But you can consume the same Fortran subroutine shown in it in Microsoft Office VBA as follows: the key aspects are ByVal attribute for the string parameter and (preferably) LongPtr type to match with preferably c_size_t kind for the integer on the Fortran side, this one too as ByVal:

 

Option Explicit
Public Declare PtrSafe Sub Fstr Lib "c:\temp\Fdll.dll" (ByVal Str As String, ByVal LenStr As LongPtr)

 

And a trivial button example below to see how the subroutine can be invoked in Excel:

 

Private Sub cmdBtn_Click()

   Dim LenStr As LongPtr
   Dim Str As String

   On Error GoTo StrErr

   Range("Str").Clear
   LenStr = Range("LenStr").Value
   If (LenStr > 0) Then
      Str = String(CLng(LenStr), " ")
      Call Fstr(Str, LenStr)
      Range("Str").Value = Str
   Else
      Range("Str").Value = xlErrNA
   End If
   Exit Sub
    
StrErr:
   Range("Str").Value = xlErrNA
    
End Sub

 

 

 

View solution in original post

2 Replies
FortranFan
Honored Contributor II
294 Views

@ripittard ,

This post may be of limited value to you since what is below is prescriptive.  But if interested, take a look at this thread which has Fortran with VB.NET (a different beast of course) : https://community.intel.com/t5/Intel-Fortran-Compiler/x64-string-passing/td-p/1373313

But you can consume the same Fortran subroutine shown in it in Microsoft Office VBA as follows: the key aspects are ByVal attribute for the string parameter and (preferably) LongPtr type to match with preferably c_size_t kind for the integer on the Fortran side, this one too as ByVal:

 

Option Explicit
Public Declare PtrSafe Sub Fstr Lib "c:\temp\Fdll.dll" (ByVal Str As String, ByVal LenStr As LongPtr)

 

And a trivial button example below to see how the subroutine can be invoked in Excel:

 

Private Sub cmdBtn_Click()

   Dim LenStr As LongPtr
   Dim Str As String

   On Error GoTo StrErr

   Range("Str").Clear
   LenStr = Range("LenStr").Value
   If (LenStr > 0) Then
      Str = String(CLng(LenStr), " ")
      Call Fstr(Str, LenStr)
      Range("Str").Value = Str
   Else
      Range("Str").Value = xlErrNA
   End If
   Exit Sub
    
StrErr:
   Range("Str").Value = xlErrNA
    
End Sub

 

 

 

ripittard
New Contributor I
269 Views

Thanks FortranFan, that gave me exactly what I was after!

Reply