1
votes

I have a .bat file calling from VBA, it is working when I use a local folder as path (example as C:\Users\cthoud01\Desktop\my scripts\scripts).

However, I got an error if I use a path from a network directory (example - H:\scripts). I also tried replacing the path as """H:\scripts\""" but continue to get the same error. I would like to hear from our experts if there is any way around to make this work.

Below is the error message I receive: enter image description here

VBA Code I am using:

Sub test()
Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim WindowStyle As Integer: WindowStyle = 1
Dim errorCode As Long


Dim pth As String
errorCode = wsh.Run("cmd.exe /k cd """ & "H:\scripts\" & """ && DeleteMatrix.bat", WindowStyle, waitOnReturn)

If errorCode <> 0 Then
    MsgBox "fail, please retry"
    End
End If

End Sub

1
See cd /? for why it doesn't work. Your cd command isn't doing what you think it is.user6017774

1 Answers

2
votes
errorCode = wsh.Run("cmd.exe /k cd /d """ & "H:\scripts\" & """ && DeleteMatrix.bat", WindowStyle, waitOnReturn)

Look at the extra /d.

In MSDos each drive had a current directory.

C:\>cd D:\dog
C:\>Copy *.* D:

would copy files into the d:\dog directory. This makes typing easier. There were no mouses or menus.

Windows only has the concept of current directory (and it's per program).

So MSDos batch files run unchanged in CMD you have to tell windows you don't want MSDos behaviour with the CD command by using /d.

Likewise CMD simulates a default drive per directory.

This is what it looks like in the process's environment block

=C:=C:\Users\David Candy\Desktop\Editor\EditorSdi
=D:=d:\~MSSETUP.T