Access VBA Shell command not working - vba

so I'm trying to get a very simple shell command to execute a batch file that's created, but for some reason the shell command doesn't do anything.
The batch file gets created and when I double click on the batch file it executes the script just fine, but if I try to call the shell command in VBA it appears that it tries to open a command window, but immediately closes it after, not producing any errors. This code used to work fine before I reformatted this laptop AND this code works on another system running Server 2012 R2 (I'm running windows 10)
Dim SedjaShell
f = "C:\Temp\_SedjaScript.bat"
Open f For Output As #1
Print #1, Chr(34) & SedjaDir & Chr(34) & " merge --files " & Page1 & " " & Page2 & " " & TOS & " " & Disclosure & " --output " & OutputDir
Print #1, "Pause"
Close #1
SedjaShell = Shell("C:\Temp\_SedjaScript.bat", 1)
Is there something I might be missing?
EDIT
Adding the contents of _SedjaScript.Bat
"C:\Users\User\Desktop\DealTracker\MergeFiles\sejda-console-3.0.4\bin\sejda-console.bat" merge --files C:\Temp\ContractSummary.pdf C:\Temp\Page2.pdf C:\Temp\TOS.pdf C:\Temp\Disclosure.pdf --output "C:\Users\User\Desktop\DealTracker\Completed Contracts\Company-Name-Inc-07-19-2017-Contract.pdf"
Pause

Thanks to Andre I was able to get the script to work inside VBA's Shell command.
By setting the PATH for my JRE to JAVA_HOME I was able to successfully launch the script.
setx JAVA_HOME "C:\Program Files (x86)\Java\jre1.8.0_141"
setx PATH "%PATH%;%JAVA_HOME%\bin";

Related

How to concatenate files by calling Windows Shell with VBA

I am trying to write some VBA in Excel that will concatenate .csv files by executing the copy and more commands in the Windows Shell.
This code successfully creates an empty file concat_target.txt (into which the others will be concatenated):
Shell ("cmd.exe copy /b NUL " & """\\path\to\network\folder\concat_target.txt""")
However, this code does not have the intended effect of saving file_1.txt as file_1.concat_temp (skipping the header):
Shell ("cmd.exe more +1 " & """\\path\to\network\folder\file_1.txt""" & " > " & """\\path\to\network\folder\file_1.concat_temp""")
In fact, every variation of the call to more that I have tried just pops up a console (that stays open) whereas the call to copy does not. The UNC network path has cursed spaces in it.
UPDATE:
It turns out that adding the /c flag to the more command does the trick (although it seems to have no effect on the call to copy). So this code is having the desired effect:
Shell ("cmd.exe /c more +1 " & """\\path\to\network\folder\file_1.txt""" & " > " & """\\path\to\network\folder\file_1.concat_temp""")
I will post the full solution for posterity once it is finished.
I don´t know why you are using 'more', but I would use other methods:
Shell ("cmd.exe /c type ""\\path\to\network\folder\file_1.txt"" >> ""\\path\to\network\folder\file_1.concat_temp""")
Notes:
redirecting with '>' will overwrite the file, losing previous contents
redirecting with '>>' will append the file (concatenate)
no need to separate every element in your string with " & " if they are all text (but I guess you already knew this)
no need to create an empty file first.
UPDATE:
OP was using 'more' to exclude the .csv table headers, as I hadn´t understood before.
So the 'type' command won´t work in this case.

How to use Excel VBA to run WinSCP Script?

I wrote a WinSCP script that upload a local file to a SFTP site. The script is saved into .txt:
WinSCP.com
open sftp://username:password#address
cd /export/home/Desktop/Temp
put C:\Users\Dekstop\JPMC\a.xlsx
close
exit
Then I look at this post and write this code into Excel VBA:
Sub RunWinScp()
Call Shell("C:\Program Files (x86)\WinSCP\WinSCP.com /ini=nul/script=C:\Users\Desktop\WinSCPGetNew.txt")
End Sub
But when I try to run it, nothing happens. There is no error, but the file is not transferred correctly neither. Could someone please help?
Thanks a lot!
You need to enclose the path to winscp.com with double quotes, as it contains spaces
You need a space between the /ini=nul and /script=...
Call Shell("""C:\Program Files (x86)\WinSCP\WinSCP.com"" /ini=nul /script=C:\Users\Desktop\WinSCPGetNew.txt")
Remove the winscp.com at the beginning of your WinSCPGetNew.txt script. There's no winscp.com command in WinSCP. You already run WinSCP.com by the Shell function in VBA. This is actually already covered in the question you link to yourself: Using VBA to run WinSCP script.
Though you better specify the commands on WinSCP command-line using the /command switch to avoid a need to for a separate commands file:
Call Shell( _
"""C:\Program Files (x86)\WinSCP\WinSCP.com"" " + _
"/ini=nul " + _
"/command " + _
"""open sftp://username:password#example.com/"" " + _
"""cd /export/home/Desktop/Temp"" " + _
"""put C:\users\Desktop\JPMC\a.xlsx"" " + _
"""close"" " + _
"""exit""")

excel vba won't execute .bat but manually executing bat works fine

Hi I have a perfectly working bat named: start.bat
containing:
start C:\Users\*user*\Documents\*path*\hidebat.vbs
and once it is manually opened it works perfectly, meaning it opens hidebat.vbs, which opens a .bat minimized which uploads files to my cloud. Hence it's verified.
I've added
pause
to the start.bat to see what it does and when I tell excel to open the start.bat it will open cmd and display the exact command as required, but it will not execute the hidebat.vbs.
I expect that there is somehow some path constraint or environment constraint when it is run from excel that prevents it to actually reach out of that limited environment.
Within excel I have tried calling the .bat in 3 different ways with:
Dim path As String
path = Application.ActiveWorkbook.path
path = path & "\"
Dim MY_FILENAME3 As String
MY_FILENAME3 = path & "start.bat"
1.
retVal = Shell(MY_FILENAME3, vbNormalFocus)
' NOTE THE BATCH FILE WILL RUN, BUT THE CODE WILL CONTINUE TO RUN.
If retVal = 0 Then
MsgBox "An Error Occured"
Close #FileNumber
End
End If
2.
PathCrnt = ActiveWorkbook.path
Call Shell(PathCrnt & "start.bat")
3.
Dim batPath As String: batPath = path
Call Shell(Environ$("COMSPEC") & " /c " & batPath & "start.bat", vbNormalFocus)
Does anybody have any clue on why it will not execute the .bat file, or what I could do to ensure it will run correctly?
Note. I think it is because it opens the default path, so I'm gonna tell it to "cd" to the actual path where the excel is saved and where the .bat files are.
Yes that was it, the path was set to some random/standard/working/current path by command, so I had to add:
Print #FileNumber, "cd " & path
to the excel macro
so that start.bat looked like:
cd *path*
start *path*\hidebat.vbs
Hope this helps future me's.

Shell Command - Bring to front

I have a program that runs a shell command to do a constant ping, but when its ran, it does not bring the cmd window to front.
This seems like it has to have some little fix I am missing.
Code:
Shell("ping " & IPAddy.Text & " -t")
Shell("ping " & IPAddy.Text & " -t", AppWinStyle.NormalFocus)
Use the Style parameter with the argument AppWinStyle.NormalFocus as shown above
MSDN: Shell Function: https://msdn.microsoft.com/en-us/library/xe736fyk(v=vs.90).aspx

Equivalent to Run Method(VBA) in OpenOffice Calc

I have been trying to convert a VBA for Excel code to OpenOffice Calc (basic) and now Im having trouble accessing an external program (and it´s database) to generate an output which I will use later in my code. In other words, I give an input, call the program and later I want an output file.
In VBA:
Prog = Worksheets("Settings").Cells(2, 2) & IPRODB & " -i " & DateiAll_in
Wait = True
Set sh = CreateObject("WScript.Shell")
RetVal = sh.Run(Prog, 0, Wait)
Worksheets("Settings").Cells(2, 2) is the program's path and IPRODB is the database's path, "-i" seems to be a command to the program and DateiAll_in is the Input file (.csv) I created before in the code.
The OpenOffice basic offers the function Shell(Pathname, Windowstyle, Param, bSync) to open external programs but It would not be the same since in the "run method" in VBA I am running a macro containing the program, its´s Database and Input File. (expression.Run(MacroName, varg1,varg2, (...))
Is there any alternative to the Shell function or can I use it in the same way as I used the "run method" in VBA?
From the OpenOffice.org forums (didn't test it myself):
Dim oSvc as object
oSvc = createUnoService("com.sun.star.system.SystemShellExecute")
Rem Launch notepad
oSvc.execute(ConvertToUrl("C:\windows\notepad.exe"), "", 0)
I used the Shell function. In the first argument of the function I added all the equivalents of "Worksheets("Settings").Cells(2, 2) & IPRODB & " -i " & DateiAll_in". I substituted Worksheets("Settings").Cells(2, 2) for the direct path of the program only because it was easier. So it worked with this code below:
Shell("C:\Program Files\*******\*****\*****.exe & IPRODB & "-i" & DateiAll_in",1,,True)