I am trying to create a macro enabled Excel file, in which, upon hitting the button, the Excel gets saved to data.csv
into the same folder and an .exe
file is to be invoked (which is again in same folder) which will access the data.csv
for its inputs.
I prepared this, it created the .csv
file, however, when the .exe
runs, it is unable to access the .csv
file (refer screenshot captured while the macro was initiated). The directory shown in that screenshot is the directory in which the .exe
was created originally - this has nothing to do with current working directory. The code is shown below. I tried running the .exe
file separately and it ran seamless, read the .csv
file properly, so no issue with exe/csv files. it is just that while running from macro, the .exe
is not searching the .csv
in the correct folder.
Public Sub Save_CSV()
Dim MyPATH As String
Dim FileNAME As String
Dim FilePath As String
Dim OldPath As String
Dim RetVal As Variant
Dim stat As Integer
'*
FilePath = ActiveWorkbook.path & "\dynjackup.exe"
OldPath = CurDir
MyPATH = ActiveWorkbook.path
FileNAME = ActiveWorkbook.Name
FileNAME = Left(FileNAME, Len(FileNAME) - 4) ' REMOVE XLS EXTENSION
'FileNAME = FileNAME & "csv" ' ADD CSV EXTENSION
FileNAME = "data.csv" ' any name can be set here
Application.DisplayAlerts = False ' REMOVE DISPLAY MESSAGE: PREVIOUS FILE WILL BE ERASED
ActiveWorkbook.SaveAs FileNAME:= _
MyPATH & "\" & FileNAME, FileFormat:=xlCSV, _
CreateBackup:=False
'
OldPath = CurDir
'
' Call ChangeCurrentDir(ActiveWorkbook.path, stat)
RetVal = Shell(FilePath, vbNormalFocus)
'Call ChangeCurrentDir(OldPath, stat)
'ActiveWindow.Close
'Application.DisplayAlerts = True ' Restore DISPLAY MESSAGE
End Sub
Error: