[英]Running .exe using VBA
所以我創建了一個使用VBA(TLSolver.xlsm)運行的.exe文件(tlsolver.exe)。 當我啟動.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
當我運行宏時,控制台窗口會彈出,然后快速關閉。 我在關閉之前設法看到了這個錯誤:
IOError: [Errno 2] No such file or directory: 'TLSolver.xlsm'
我知道當我經常雙擊文件時,.exe工作得很好,所以我傾向於認為我在VBA中搞砸了一些愚蠢的東西。
任何幫助贊賞!
編輯:我知道sub被標記為StartExeWithArgument,但是不需要參數,只需單擊並運行即可。
shell命令正在正確執行。 exe
啟動然后在當前路徑中查找.xlsm
文件。 發生的事情是它無法在當前目錄中找到TLSolver.xlsm
,因此錯誤IOError: [Errno 2] No such file or directory: 'TLSolver.xlsm'
在這種情況下有三個建議。
使用ChDir
將VBA中的目錄更改為excel文件所在的目錄,然后啟動exe OR
將兩個文件放在同一目錄中。 要么
重寫python exe代碼( 這不符合我的專業知識,因此無法幫助你 )提示用戶選擇excel文件。
VBA PART(建議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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.