1

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:

enter image description here

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
RK R
  • 11
  • 1
  • 4
  • 8
    Dont link to the code, paste it **here**. – A.S.H May 08 '17 at 12:57
  • Try adding `ActiveWorkbook.Close (False)` before you call `Shell`. Most likely excel has it opened exclusively – Nick.Mc May 09 '17 at 06:05
  • @Nick.McDermaid I think this wont work because the macro is running from ActiveWorkbook and will cancel immediately after closing and before he can run shell. – Pᴇʜ May 09 '17 at 06:08
  • If this is the solution to the issue then I guess it needs to run from a different workbook. – Nick.Mc May 09 '17 at 06:10
  • Possible duplicate of [How to use VBA SaveAs without closing calling workbook?](http://stackoverflow.com/questions/18899824/how-to-use-vba-saveas-without-closing-calling-workbook) – Pᴇʜ May 09 '17 at 06:30
  • gents, thanks for all that comments.. if you see the screenshot - you could understand that the .exe file is called and it has started as usual but it is looking into a directory (c:\dyn-jack\lower\massparti-endce-fxd) for the .csv file. i dont know why the exe is looking into this directory. this was the directory in which the exe was compiled. but if i run the exe file by double clidking it, there is no issue, it finds the .csv file and the whole program works error free. i believe the issue is while running the exe through macro, it is not able to locate the .csv file – RK R May 09 '17 at 11:16

1 Answers1

1

Probably the most stable solution would be modifying dvnjackup.exe so it takes the complete absolute filename of the desired data file as a parameter. You could then pass this path in your Shell() call.

RetVal = Shell("""C:\dvnjackup.exe"" ""C:\Full\Path to\data.csv""", vbNormalFocus)

Note that I escaped both paths using quotes (the literal "" in the string gives you a " in the result).

If you can't modify dvnjackup.exe you can try explicitly setting the working directory. But it seems you uncommented some code that does this (see the ChangeCurrentDir calls).

Anyways, you can change the current working directory like this:

ChDir "C:\Path\To New Working\Directory"
' No need to enclose paths containing spaces here

Hope this helps! :)

Marcus Mangelsdorf
  • 2,852
  • 1
  • 30
  • 40