使用VBA运行.exe

所以我创build了一个.exe文件(tlsolver.exe)来运行使用VBA(TLSolver.xlsm)。 当我启动.exe时,它运行一些计算,吐出一个csv文件,然后使用VBA将这些数据复制到我的Excel工作表上。

这是我正在使用的VBA代码:

Public Sub StartExeWithArgument() Dim strProgramName As String ActiveWorkbook.Save strProgramName = "C:\Users\My.Name\Desktop\Python\Tagless\tlsolver.exe" Call Shell("""" & strProgramName & """", vbNormalFocus) End Sub 

当我运行macros时,控制台窗口会popup,然后快速closures。 在closures之前,我设法看到这个错误:

 IOError: [Errno 2] No such file or directory: 'TLSolver.xlsm' 

我知道当我双击文件时,.exe工作得很好,所以我倾向于认为我在VBA中搞砸了一些愚蠢的东西。

任何帮助感激!

编辑:我知道子被标记为StartExeWithArgument,但没有必要的参数,只需点击并运行。

shell命令正确执行。 该exe启动,然后查找当前path中的.xlsm文件。 发生了什么事是它无法find当前目录中TLSolver.xlsm ,因此错误IOError: [Errno 2] No such file or directory: 'TLSolver.xlsm'

在这种情况下有三点build议。

  1. 使用ChDir将VBA中的目录更改为excel文件所在的目录,然后启动exe

  2. 将这两个文件放在同一个目录中。 要么

  3. 重写python的exe代码( 这是我的专业知识,所以不能帮你在这里 )提示用户selectexcel文件。

VBA部分(build议1)

 Public Sub StartExeWithArgument() Dim strProgramName As String Dim xlFilePath As String '~~> Path of the excel file. Change as applicable xlFilePath = "C:\Temp" ActiveWorkbook.Save '~~> Change directory ChDir xlFilePath strProgramName = "C:\Users\My.Name\Desktop\Python\Tagless\tlsolver.exe" Call Shell("""" & strProgramName & """", vbNormalFocus) End Sub