如何在创建文件的情况下运行.exe文件?

时间:2014-12-31 10:08:45

标签: excel-vba vba excel

如何在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

1 个答案:

答案 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