-1

I does anyone know how to wait for a shell command to finish running before moving on? My code is throwing a "Run-time error '1004': Sorry, we couldn't find PC_one.csv. Is it possible it was moved, renamed or deleted". The file gets created and give that error. if i run the sub again, it opens the file.

Dim sCommandToRun As String
sCommandToRun = "systeminfo /fo csv > PC_one.csv"
Call Shell("cmd.exe /S /C" & sCommandToRun, vbHide)
Workbooks.Open ("PC_one.csv")
  • Does this answer your question? [Wait for Shell to finish, then format cells - synchronously execute a command](https://stackoverflow.com/questions/8902022/wait-for-shell-to-finish-then-format-cells-synchronously-execute-a-command) – Vincent G Aug 19 '21 at 14:36
  • https://stackoverflow.com/questions/15951837/wait-for-shell-command-to-complete – Nathan_Sav Aug 19 '21 at 14:37
  • unfortunately neither of those work. VincentG, msgbox returns the error message. Nathan_Sav, command doesnt run – Jacob Mazanowski Aug 19 '21 at 14:46
  • I think both answers in the link were the same?? Can you post the revised code too. – Nathan_Sav Aug 19 '21 at 14:56
  • "if i run the sub again, it opens the file" - possibly this is because you set your current directory to the file location after the first failed run... – Tim Williams Aug 19 '21 at 15:47

1 Answers1

2

Specify the folder as well as the filename.

Option Explicit

Sub info()

   Dim wsh As Object, sCommandToRun As String, Filename As String
   Set wsh = VBA.CreateObject("WScript.Shell")

   Filename = ThisWorkbook.Path & "\PC_one.csv"
   sCommandToRun = "cmd.exe /S /C systeminfo /fo CSV > """ & Filename & """"
   'Debug.Print sCommandToRun

   wsh.Run sCommandToRun, 1, 1
   Workbooks.Open Filename

   MsgBox "Done"
  
End Sub
CDP1802
  • 13,871
  • 2
  • 7
  • 17