Call shell command in Excel 2010 VBA - vba

When I call a shell command in vba, the program opens as does the command prompt but nothing happens and no results are obtained. I can not seem to figure it out and need some help please.
Command that works
C:\Program Files\BioDiscovery\ImaGene 9.0>ImaGene.exe -batch "C:\Users\cmccabe\Desktop\EmArray\Design\test_11_19_2015.bch"
My attempt in excel 2010 using VBA (which opens the program and command prompt, but does not execute the command).
Dim Par As String
Par = "dir c:\Program Files\BioDiscovery\ImaGene 9.0\ImaGene.exe -batch "C:\Users\cmccabe\Desktop\EmArray\Design\imagene.bch"
Call Shell("C:\WINDOWS\system32\cmd.exe /c " & Par, 1)
MsgBox ("ImaGene analysis complete")

May not be the best answer but the below solution works for me for similar situation.
open a notepad "c:\Program Files\BioDiscovery\ImaGene 9.0\ImaGene.exe" -batch "C:\Users\cmccabe\Desktop\EmArray\Design\imagene.bch"
save as say "test.bat"
In excel call this .bat file
Dim wshell As Object
Set wshell = CreateObject("wscript.shell")
wshell.Run Chr(34) & "full path\test.bat"
Hope this helps.

Related

"Access is denied" Run VB script from Excel Macro

I need to make an asynchronous job. In loop from MAIN.xlsm I want to call VB script (with different parameters) to open more Excel instances where .refreshAll method is executed. (This way I can open more files at once and don't need to wait on synchronous execution of VBA in Excel).
The last thing causing problem is permissions in Shell. To make it easier I simplified code (w/o loop & parameters). Sub OpenShell is in Excel VBA module. UAC is off, I am local admin on PC.
Sub OpenShell()
strShell = "cmd.exe /k cscript " & ThisWorkbook.Path & "\1.vbs"
Shell strShell
Exit Sub
VB script has only one line: Wscript.Echo "Running"
Result in shell: "Access is denied".

VBA - Execute or Run .bat File Inside Macro

I have been attempting to automate a series of administrative events for some of the users where I work by creating scripts and macro's and so on..
These scripts and macros work great, however, I would like to make a the process even easier for the users by running a single batch file that will systematically execute the scripts and macros.
The batch file I currently have, calls all the scripts one by one, and the very last script opens one of the xlsm workbooks which contains a few macro's in it - and here is where the issue is - There are still scripts to be executed but they can only be executed once this workbook has executed all its macros.
So my initial thought was to test if the workbook is open, if it is, delay the execution of the next script by a minute or so, then test again and again... until it is closed.. Then I thought perhaps it would be easier to execute the next set of scripts (also in a batch file) from within a macro.
So, I have this code:
Sub Run_BAT()
Set obj = CreateObject("Wscript.Shell")
obj.Run Chr(34) & "X:\Test\" & "Termination Reports Scripts\" & "Execute_Terminations.bat" & Chr(34), 0, True
Set obj = Nothing
End Sub
Which gives me an error:
Permission Denied
Then there's this code:
Sub WriteAndRunBatFile()
Call Shell("X:\Test\Termination Reports Scripts\Execute_Terminations.bat")
End Sub
Which gives me the error:
Invalid procedure call
Any and every single code sample that contains the "Shell" command gives this error.
Place your path to bat file in quotes:
Call Shell("cmd /c ""S:/somebatfile.bat""", vbNormalFocus)
Or
Call Shell("cmd.exe /C /K " & "ChDir X:\Test\Termination_Reports_Scripts && Execute_Terminations.bat", vbNormalFocus)
And yes, check permissions.
My theory is you're missing a reference in your application to the Windows Script Host Object Model.
In the VBA Editor, go to Tools, References, make sure that one's ticked.
It's not ticked by default for security reasons - imagine unintended access to the command prompt in every instance of a Microsoft Office application...!
(1) Check permission of user of that X directory.
(2) Test the code by removing spaces from directory name.
Also try the following code (Please try it by removing spaces from directory name).
Sub Button1_Click()
Call Shell("CMD.EXE /C " & "X:\Test\Termination_Reports_Scripts\Execute_Terminations.bat")
End Sub

Running Macro from .bat with another Access db open

I need to run a few Access macros automatically periodically throughout the day, I do this by scheduling a batch file to open the relevant Access db and run the macro.
The problem that I am having is, if I am working on another Access database and therefore already have an instance of Access open, the batch file runs but only opens the database containing the macro that is supposed to run and stops. So the macro doesn't run.
Has anyone else had this issue or know how to solve it?
The batch files that task scheduler calls look like this:
start /min "C:\Program Files\Microsoft Office 15\root\office15\MSACCESS.EXE" "Q:\TC\DNI_Updater\DNIUPDATER.accdb" /X DailyUpdate
I could not find a way to get the DailyUpdate macro to run in the second Access session when using start /min to open that session.
I'm still unsure whether it's possible, but I gave up and switched to VBScript instead. With that approach, it's easy to start the second Access session minimized and run the macro ...
Option Explicit
Dim AccessExePath, DbPath, CmdLine, objShell
DbPath = "C:\Users\hans\Documents\test_hfu.accdb"
AccessExePath = "C:\Program Files (x86)\Microsoft Office\Office14\MSAccess.exe"
CmdLine = """" & AccessExePath & """ """ & DbPath & """" & " /X DailyUpdate"
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run CmdLine, 7 ' Minimize. The active window remains active.
If this approach is satisfactory, you can use the script file as your scheduled task's "Start a program" Action property ...

Excel VBA Open and close a Folder

So I know how to open a folder within an excel vba macro, but I want to be able to close it as well. Any thoughts? This is what I'm using to open the folder:
Shell "Explorer.exe *file path*"
You can use the Process ID, stored in vPID, to kill the application when you’re done with it. all you have to do is invoke the TaskKill command with the same Shell function.
Dim vPID As Variant
vPID = Shell "Explorer.exe *file path*"
'Perform actions here
'// Kill file
Call Shell("TaskKill /F /PID " & CStr(vPID))

Shell *.vbs from within VBA

Hi can anyone tell me how to execute my .vbs from within VBA cause the following code dose not work.
RetBat4 = Shell("c:\VTS\QUEEN ANNES REVENGE\SYSTEM\VBS\UNDO_2.vbs", 1)
VBA debug says that the sytax is wrong????
thanks
Try
Shell("cscript ""c:\VTS\QUEEN ANNES REVENGE\SYSTEM\VBS\UNDO_2.vbs""",1)
If your VBS needs complete command shell environment, use this:
Shell("cmd /c cscript ""c:\VTS\QUEEN ANNES REVENGE\SYSTEM\VBS\UNDO_2.vbs""",1)
and if your program should wait until the VBS ends, read this post:
VBA Shell and Wait with Exit Code
try This:
Set ws=CreateObject("Wscript.shell")
set ws.exec("ping " & ip & " -n 20")
set ws=nothing