如何在excel VBA中编程以运行外部程序,外部程序能够将收集的数据输出到文件中?我的外部程序voltage.exe在正常运行时(双击桌面屏幕上的程序)将收集的数据输出到文件data.txt中。但是,当使用下面的代码运行时,没有创建文件data.txt。
Sub Button1_Click() ' run logger
Dim path As String
path = ActiveWorkbook.path
path = path + "\Voltage Recording.exe"
retval = Shell(path, vbNormalFocus)
End Sub
答案 0 :(得分:2)
您的文件是在当前目录中创建的,您可以按功能获取其路径:CurDir()
您可以通过调用SetCurrentDirectory
函数来更改当前目录:
Declare Function SetCurrentDirectory Lib "kernel32" Alias "SetCurrentDirectoryA" (ByVal lpPathName As String) As Long
Sub Button8_Click()
MsgBox ("Old Dir = " & CurDir())
SetCurrentDirectory ActiveWorkbook.path
MsgBox ("Current Dir = " & CurDir())
Dim path As String
path = ActiveWorkbook.path
path = path + "\Voltage Recording.exe"
MsgBox (path)
retval = Shell(path, vbNormalFocus)
End Sub