Open Explorer Maximized with Focus and Highlight File from Access VBA - vba

I am trying to open Windows Explorer and highlight a file.
I am using:
Private Sub cmdLoadStl_Click()
Dim shellCmd As String
shellCmd = "explorer.exe /select, """ & Me.txtPath & """,vbMaximizedFocus"
Debug.Print shellCmd
Shell (shellCmd)
End Sub
This works as expected except the window opens minimized.
I have tried this on several Win7 machines running Access 2016 and two Win10 machines, one running Access 2013, the other running Access 2016.
Is there any way to get the window to open maximized?
Edit: More information - If I paste the command generated by debug.print in my code to a command prompt and run it, the explorer window opens maximized, with the file highlighted, as it should.
The command generated from vba looks like this.
explorer.exe /select, "C:\Users\user\Desktop\filename.txt",vbMaximized
Thanks.

Try like this:
shellCmd = "explorer.exe /select, """ & """,vbMaximizedFocus
shell shellcmd,vbMaximizedFocus
The second (optional) algorithm of shell is the focus. vbMaximizedFocus opens it maximized:

Related

How can I open a separate process in file explorer and search using vba

I'm Using MS Access and VBA.I'm trying to open a separate process in windows file explorer and then search for a file.
Using:
vPID = Shell("""explorer.exe""" & Chr(34) & """search ms:query=*.pdf&crumb=location:c:\""" & Chr(34),vbNormalFocus)
I can open explorer and search.
Using:
vPID = Shell("explorer.exe /separate", vbNormalFocus)
I can open explore as a separate process
If I add the switch /separate to the code then the search doesn't run.
Does anyone know how I can combine the two to work together i.e. open file explorer as a separate process and run a search? I've tried every combination I can think of

Running an excel macro from task scheduler

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.

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

VBA - Hide Chrome window

I have a vba routine in excel that downloads something with chrome. But I'd like to stop the chrome window from opening in front of me and instead have it run in the background, keeping the focus on my excel file.
The code looks like this :
Sub dl()
Dim WebUrl As String
WebUrl = [J1]
Shell ("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe -url " & WebUrl)
End Sub
I've heard of shell functions (with vbMinimizedNoFocus) which I think could do the trick but I can't find anywhere the correct syntax and an example of how to apply it...
Can anyone help me ?
Thank you very much in advance !
The documentation for Shell defines vbMinimizedNoFocus as 6 and gives the syntax:
Shell "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe -url " & WebUrl, vbMinimizedNoFocus ' Should be defined in the global namespace

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