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 ...
Related
I am trying to run a macro using task scheduler and it looks like it executes the VBScript file but it doesn't actually execute any of the code in the macro.
The pathway for the workbook is
C:\Users\cdurrell\Desktop\Test Auto 1.xlsm
The macro is called TestAuto
And the Script file has the pathway
C:\Users\cdurrell\Desktop\TestAutoScript.txt
I originally was using the following script file
'Write Excel.xls Sheet's full path here
strPath = "C:\Users\cdurrell\Desktop\Test Auto 1.xlsm"
'Write the macro name - could try including module name
strMacro = "Update" ' "Sheet1.TestAuto"
'Create an Excel instance and set visibility of the instance
Set objApp = CreateObject("Excel.Application")
objApp.Visible = True ' or False
'Open workbook; Run Macro; Save Workbook with changes; Close; Quit Excel
Set wbToRun = objApp.Workbooks.Open(strPath)
objApp.Run strMacro ' wbToRun.Name & "!" & strMacro
wbToRun.Save
wbToRun.Close
objApp.Quit
'Leaves an onscreen message!
MsgBox strPath & " " & strMacro & " macro and .vbs successfully completed!", vbInformation
'
and it would make the excel workbook read only and not execute the code in the macro.
Then I took out the line
wbToRun.Save
and now it doesn't make the excel workbook read only but it still doesn't execute the code.
Any pointers or corrections are welcome! Thanks!
I'm not sure if you are still facing this issue, but I found my own solution since I was experiencing the same trouble. It is based on this old answer in Super User by squillman:
https://superuser.com/a/579901
Open Component Services (Start -> Run, type in dcomcnfg)
Drill down to Component Services -> Computers -> My Computer and click on DCOM
Config
Right-click on Microsoft Excel Application and choose Properties
In the Identity tab select This User and enter the ID and password of an interactive user account (domain or local) and click OK
Now, what else I needed to do?
Configure the scheduled task with the same account that I configured in step 4. I tried with the service account and failed.
Run whether user is logged or not.
After this, I was able to run it properly.
In my case it started working once I checked 'run with administrative privileges' in the task settings.
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".
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
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.
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.