[英]open .exe in VBA excel and write in the input window
我必须在 VBA Excel 中运行 an.exe 并写入输入 window "in.txt" "out.txt" 以使该过程在宏内自动进行。 我尝试使用 shell 但它异步工作,我也不知道如何告诉她在 .exe 中写入。
我也尝试过使用 SendKeys 但显然它不起作用。 How could I make the VBA calling my.exe, open it, write inside the command window of the.exe, wait for the output and use it to go on?
先感谢您
这是两次尝试(均失败):
Sub write()
prog = Shell("C:\Users\arancia\Pictures\Camera Roll\axtur\axtur\AXTUR_64.exe", 1)
Application.Run "'AXTUR&EXCEL.xlsm'!inserisci_dati_input"
SendKeys.send "in.txt~", True
SendKeys.send "out.txt~", True
SendKeys "%{F4}", True
End Sub
Sub StartExeWithArgument()
Dim strProgramName As String
Dim strArgument As String
strProgramName = "C:\Users\arancia\Pictures\Camera Roll\axtur\axtur\AXTUR_64.exe"
strArgument = "in.txt~out.txt~"
Call Shell("""" & strProgramName & """ """ & strArgument & """", vbNormalFocus)
End Sub
一种解决方案是编写一个包含所有参数的批处理文件,然后运行该批处理。
我过去曾使用 WshShell(Windows 脚本主机)运行批处理文件来执行您想要的操作,但自 2020 年 11 月更新以来,WshShell 无法在我们的计算机上运行。 WshShell 允许您等待外部程序的结果。
我发现 go 的一种方法是在批处理结束时编写一个简单的文本文件并等待它出现。 这很粗糙,但它有效。
在下面的代码中,我在 Excel 表的文件夹中编写了一个简单的批处理文件。 批处理的最后一行将文件夹的内容写入文本文件。 Do Until
循环等待文本文件以 1 秒为增量显示。 当代码在循环后恢复时,文本文件被删除。 如果您编写命令行,您将输入 cmd 而不是“echo Hello World”,这应该可以工作。
您需要参考 Microsoft Scripting Runtime (scrrun) 才能使用文件系统 object。
祝你好运!
Public Sub RunBatch()
Dim i As Integer
Dim k As Integer
Dim xlWB As Workbook
Dim fso1 As New FileSystemObject
Dim BatFile As Object
Dim IsDone As Boolean
Dim OutFileName As String
Set xlWB = ThisWorkbook
OutFileName = xlWB.Path & "\" & "HW.bat"
Set BatFile = fso1.CreateTextFile(OutFileName)
BatFile.WriteLine "cd /d " & xlWB.Path
BatFile.WriteLine "echo Hello World"
BatFile.WriteLine "dir > Done.txt"
BatFile.Close
IsDone = False
Call Shell(OutFileName, vbNormalFocus)
Do Until IsDone
If fso1.FileExists(xlWB.Path & "\Done.txt") Then IsDone = True
Application.Wait (Now + TimeValue("00:00:01"))
Loop
fso1.DeleteFile (OutFileName)
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.