similar to How do you run a .exe with parameters using vba's shell()?
Sub RunExe()
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
errorCode = wsh.Run("C:\dir\dir\dir\dir\dir\dir\dir\dir\dir\My.exe", windowStyle, waitOnReturn)
End Sub
The issue I am having is that the cmd line already starts in a folder with a different path. So when I run this code it starts down a path that I know the program is not in, I get the error:
Unhandled Exception: System.IO.DirectoryNotFoundException: Could not find a part of the path
'C:\dir\dir\dir\dir\dir'.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.Directory.SetCurrentDirectory(String path)
at ReadDirectory(String directory)
at Program.Main(String[] args)
Is there some way to start back at c:\users? Or clear/flush the stream?
This is where running run -> cmd.exe starts. But for some reason I have some memory in vba and starts in a different file path.
Related
We recently upgraded to Windows 10. This VBA code was working in Windows 7 to run a batch script, but now it throws "Run-time error '70': Permission denied.
I've tried several variations of the script, but continue to run into issues. I have Administrator rights on my computer as well. This is on a network drive, but it hasn't been an issue before. Using a cmd prompt, I am able to call this batch file successfully. Any ideas or help you provide would be appreciated.
Sub DailyBatchProcess()
Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
wsh.Run Chr(34) & "S:\Data\Daily Items\1 Daily Batch\BAT\Transaction Adjustments.bat" & Chr(34), windowStyle, waitOnReturn
Set wsh = Nothing
End Sub
I am having an issue finding/understanding how to solve the following issue when running a .bat file in VBA in Excel. I am getting an Open File - Security Warning prompt when I try and execute it.
The issue is i don't have administrative privileges, so I can't set .bat files as being ignored from this prompt, and this .bat file is one that I created myself so I don't understand why my computer is flagging it as potentially harmful.
If anyone could explain a workaround, and why such an issue arises despite it being a custom generated .bat file, I would greatly appreciate it. By a workaround I mean modifying the code I currently have to deal with this problem, without having to go through Administrators.
Below is the snippet of code pertaining to the problem, thanks:
sub test()
'some stuff
Open MY_FILENAME For Output As #FileNumber
Print #FileNumber, FileContents
Print #FileNumber, "exit"
Close #FileNumber
'run batch file
Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
Dim rtn As Integer
rtn = wsh.Run(Chr(34) & MY_FILENAME & Chr(34), windowStyle, waitOnReturn)
end sub
I should add that if run the shell like so I have no issues:
rtn = Shell(MY_FILENAME, vbNormalFocus)
Except that the code will continue running despite the shell not closing and completing its execution. I need the shell to halt until it has executed it's task.
I have used the various posts similar to this and come up with the following, but not sure why this doesn't work for me. I have a UserForm wherein I am trying to run a script and capture the output, including any errors. However, the console window quickly disappears as soon as I run this code:
Sub test()
Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
command = "python ""C:\test1.py --arg1=ABC"" > ""C:\logs\test.log"""
wsh.Run command, windowStyle, waitOnReturn
End Sub
The script runs just fine by itself until I add the redirection ('>'). My python script needs to be launched with 'python' keyword preceded in the command as shown above and so I cannot use 'cmd /c'. Is it possible to capture the output without using 'cmd /c' and while using wsh.Run (preferably) instead of wsh.Exec?
Thanks.
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:
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
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
This VBA code I wrote runs in Windows 7 but not in XP.
Public Function WShellRun(command As String)
Dim wShell As Object
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 0
Dim errorCode As Integer
Dim wExec, rslt As String
Set wShell = CreateObject("Wscript.Shell")
wShell.CurrentDirectory = ActiveWorkbook.Path
On Error GoTo WinXpMode
errorCode = wShell.Run(command, windowStyle, waitOnReturn)
If errorCode <> 0 Then
MsgBox "SHELL COULDN`T STARTED " & errorCode, vbCritical, "SHELL ERROR"
Exit Function
End If
Set wShell = Nothing
Exit Function
WinXpMode:
Set wExec = wShell.Exec("%ComSpec% /c " & command) ''(1) execute DOS
Do While wExec.Status = 0 ''(2) wait until response
DoEvents
Loop
'rslt = wExec.StdOut.ReadAll ''(3) show result
'MsgBox rslt
Set wExec = Nothing
Set wShell = Nothing
End Function
I thought WSH doesn't run on XP so I wrote a GOTO to which the code would flow when something happened to the script. But when I run it in XP environment, the WinXPMode part and the upper part runs parallel that it freezes the flow of the program (I think).
I have two questions.
Is there any way to upgrade the code to work in both Windows 7 and XP so I don't have to worry about the environment.
Is there any way to check what is wrong with the code or the system?
I found that when I run the .jar for few files (10 or 20 file names), it goes without any problem. After that I tried for many files like 100 file names. Then it showed me an error that it can't output file (can't write file), which doesn't show up in Windows 7.