0

I have a VB6 EXE program to get a substring from a string passed inside VBA. How do I capture inside VBA the value of extract (I'm not interested in the VBA Mid function)

in VB6

Private Sub Main()
 Extract (command$)
End Sub
Function Extract(StrKey As String)
    LastPoss = InStr(StrKey, "_") + 1
    Extract = Mid(StrKey, LastPoss, Len(StrKey))
End Function

In VBA

Sub test()     
 aaa = "c:\EzPasteImages\Project1.exe  "**strong text**     
 ccc = "ghhfghfgh_hgfhg"  'the parameter
 go = aaa & " " & ccc    
 RET= Shell (go)
End Sub
user3387046
  • 389
  • 1
  • 3
  • 13
  • 1
    Maybe https://stackoverflow.com/questions/2784367/capture-output-value-from-a-shell-command-in-vba and http://www.vbi.org/Items/article.asp?id=4 – Tim Williams Mar 06 '20 at 22:29
  • 1
    You understand that VB6 (a compiler and forms package) hosts the VBA language. So `Mid` is exactly the same. –  Mar 08 '20 at 18:29
  • Does this answer your question? [Capture output value from a shell command in VBA?](https://stackoverflow.com/questions/2784367/capture-output-value-from-a-shell-command-in-vba) – StayOnTarget Mar 09 '20 at 11:49

1 Answers1

0

Solving your problem requires a communication mechanism between the EXE and VBA. Currently, the EXE is doing nothing with the result of the Extract method. In order to get the result back to VBA, place the result into StdOut.

Here's how to do this on the EXE side:

Option Explicit

Private Sub Main()
   Dim e As String
   e = Extract(Command)

   Dim fso As FileSystemObject
   Set fso = New FileSystemObject
   Dim stdOutput As TextStream
   Set stdOutput = fso.GetStandardStream(StdOut)
   stdOutput.WriteLine e
End Sub

Private Function Extract(ByVal StrKey As String) As String
   Extract = Mid(StrKey, InStr(StrKey, "_") + 1, Len(StrKey))
End Function

On the VBA side, here's how you get the result from StdOut:

Option Explicit

Public Sub Test()
   Dim ret As String
   ret = ShellRun("c:\TEMP\Extract\Extract.exe" & " " & "ghhfghfgh_hgfhg")
   MsgBox ret
End Sub

Public Function ShellRun(sCmd As String) As String
   'run a shell command and return stdout as a string
   Dim oShell As Object
   Set oShell = CreateObject("WScript.Shell")
   Dim oExec As Object
   Set oExec = oShell.Exec(sCmd)
   Dim s As String
   s = oExec.StdOut.ReadAll
   ShellRun = Left(s, Len(s) - 2)
End Function

The ShellRun code came from this link.

Brian M Stafford
  • 6,742
  • 2
  • 15
  • 23
  • If an answer has solved your question please consider [accepting it](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark and upvoting by clicking the up arrow. This indicates to the wider community that you've found a solution. There is no obligation to do this. – Brian M Stafford Mar 17 '20 at 16:40