"Access is denied" Run VB script from Excel Macro - vba

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".

Related

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 ...

Call shell command in Excel 2010 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.

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)

Pass Variable from Excel Macro to .vbs file

I have an excel button with a macro. The code simply stores the path of the workbook in a vaiable names 'MyCompletePath' and then runs a .vbs file. The code is:
MyCompletePath = ActiveWorkbook.FullName
'Run VBS file
Shell "wscript C:\Users\name\Desktop\vb.vbs", vbNormalFocus
I want to pass the variable 'MyCompletePath' from the excel file to the file which is executed using the last line.
I made some searches but didn't fully understand what their solutions do. Maybe someone can tell me how to do it.
UPDATE/EDIT: I'm now having a problem with the filepath after Shell "wscript. It has spaces in the folder name. How can I get it to work with spaces in the name?
Thank you.
You can include command line arguments after the path to your VBS file. For example:
Shell "wscript C:\Users\name\Desktop\vb.vbs BlahBlahBlah", vbNormalFocus
Then inside your VBS script, you can access them using the WScript.Arguments collection. For example:
MsgBox WScript.Arguments(0)
would pop up a message box displaying "BlahBlahBlah".
For a file path, as you indicated, which might include spaces and thus would be treated as multiple arguments to the script, I would include the argument in quotes, like this:
Shell "wscript C:\Users\name\Desktop\vb.vbs ""this has multiple words""", vbNormalFocus
based on passing argument from vba to vbs
When I write that within the vba:
Sub Macro1()
MyCompletePath = "toto"
Shell "wscript D:\\vb.vbs " & MyCompletePath
End Sub
and this in vb.vbs
MsgBox("Hello " & WScript.Arguments(0))
I do get "Hello toto"