【问题标题】:Pass a parameter from VB6 exe program to VBA将参数从 VB6 exe 程序传递到 VBA
【发布时间】:2020-03-06 22:19:22
【问题描述】:

我有一个 VB6 EXE 程序从 VBA 中传递的字符串中获取子字符串。 如何在 VBA 中捕获 extract 的值 (我对 VBA Mid 函数不感兴趣)

在 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

在 VBA 中

Sub test()     
 aaa = "c:\EzPasteImages\Project1.exe  "**strong text**     
 ccc = "ghhfghfgh_hgfhg"  'the parameter
 go = aaa & " " & ccc    
 RET= Shell (go)
End Sub

【问题讨论】:

标签: vba vb6


【解决方案1】:

解决您的问题需要 EXE 和 VBA 之间的通信机制。目前,EXE 对 Extract 方法的结果不做任何事情。要将结果返回到 VBA,请将结果放入 StdOut。

这是在 EXE 端执行此操作的方法:

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

在 VBA 方面,从 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

ShellRun 代码来自 this link

【讨论】:

  • 如果某个答案解决了您的问题,请通过单击复选标记考虑accepting it,并通过单击向上箭头进行投票。这向更广泛的社区表明您已经找到了解决方案。没有义务这样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-02
  • 2013-08-24
  • 1970-01-01
  • 2011-09-02
  • 1970-01-01
  • 2021-06-16
相关资源
最近更新 更多