Recently the IT policy at my work changed, and all scripts I have been using (.bat .cmd .py) no longer work. Where possible I have converted these to VBA macros that run in workbooks - as these are still enabled.
Here are two macros to schedule a force shutdown/ cancel it (respectively)
Private Sub Workbook_Open()
Application.Visible = False
Shell "SHUTDOWN /s /f" '/Scheduled shut down, /Forced (ignores any unsaved)
End Sub
And in a 2nd workbook
Private Sub Workbook_Open()
Application.Visible = False
Shell "shutdown /a" '/Abort scheduled shutdown
Application.Quit
End Sub
As you can see the macros run on Workbook_Open() events, with Application.Visible = False. The intent is for a desktop shortcut to open these workbooks as invisibly and quickly as possible.
Issues & solutions
It's slower - I used to run this as a batch script, now Excel has to open to run it
I've made it an .xlsb to reduce opening time, is there a faster method?
I'm not asking how to speed up my workbook, I just mean is there a different way of saving the file (an addin perhaps?) which can run without Excel opening?
I've also only chosen to close Excel when shutdown is aborted, not when it is scheduled, as if Excel is already open then the cancel macro opens faster (which is essential if I accidentally schedule shutdown)
The running is not invisible; although the workbook itself is never shown, the Excel startup image appears
I have tried adding switches to the shortcut target (/e to prevent standard template loading /r for readonly in the hope that it wouldn't display the sheet) - but they don't work
"C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE" /r /e "C:\Users\USERNAME\Documents\start.xlsb"
The Application calls are Excel-wide. Sometimes, I schedule a shutdown before I've saved everything. (I know it will take me a few seconds to save so I schedule shutdown first)
That was fine when I ran these as .bat/.cmd scripts, but if I run these and Excel is open then everything closes (or becomes invisible - although I have no way of checking which one), which is not ideal when I'm still working on something.
Can I make sure these only hide themeselves? Would opening another instance of Excel help?
TL;DR
I've saved macros as Workbook_Open() events in .xlsb workbooks, which run from desktop shortcuts
How can I improve on this in
Making the process hidden
Making the process faster
Preventing the process screwing up the rest of Excel
By
Flags in the desktop shortcut
Changes to the macro code
Changing how the macro is saved
And
Without just converting to scripts as they are blocked
NB, I would do this with VBS to circumvent Excel entirely, but that is blocked too
Fastest is a desktop shortcut with the shutdown command, i.e. %WINDIR%\system32\shutdown.exe /s /f
Related
When I start up RStudio on Windows, it lost focus. I have to click on it before I could input anything to the console or script window. I know that Ctrl-1 to focus on the Script Window, and Ctrl-2 to focus on the console window; however, both of these two shortcuts will work as long as I click on RStudio first.
Is there anything I need to set in RStudio?
Software Environment:
RStudio Version 0.99.446
Windows 7 Ultimate 64-bit
As a kludge, you can write a VBScript which first opens RStudio and then switches the focus to it:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.CurrentDirectory = "C:\Program Files\RStudio"
WshShell.Run """C:\Program Files\RStudio\bin\rstudio.exe"""
WScript.Sleep 200 'milliseconds
focused = WshShell.AppActivate("RStudio")
i = 1
Do While Not focused And i < 10
WScript.Sleep 100
focused = WshShell.AppActivate("RStudio")
i = i + 1
Loop
Save this to the desktop in a file with a name like ToRStudio.vbs. Then, when you click on the icon, it opens up Rstudio and then tries to repeatedly switch focus to the RStudio window. In my original version of the code, I relied on a fixed time delay before issuing a single call to AppActivate. But, I discovered that when my workspace saved from my last session was large then my VBScript would finish executing before RStudio was ready. With the above loop, it repeatedly tries to grab focus before timing out. Since I put in the loop, it has had a 100% success rate in opening RStudio with the proper focus. In some debugging code I removed, the variable i never once got above 3 in 10 consecutive attempts.
Obviously, you might have to change the path names, and tweak the timings.
In my comment above I said that certain things about how RStudio handled focus struck me as counterintuitive. Since then I discovered RStudio's option to automatically switch focus to console on sourcing. The lack of doing this by default is what I had in mind. The issue in this question was the only remaining focus issue for me. I'm somewhat used to it by now, so I'm not sure if I'll actually use this VBS script.
Thank you for any/all help, I am a newbie at batch files but I am trying to find a way to create a batch file that I can install on my end users computer that will clear onenote cache they using resources when they leave their OneNote open for a significant amount of time since they pretty much use onenote all day for information documentation and retrieval. to get to it you would type in the run window onenote /safeboot, but I can't get a batch to do that and maybe it can't be done.
this is probably not a task for a batch file, but for a scheduled task (just use its wizard to create one). set it to run regularly however convenient to you, eg. at login, or at 12am.
This microsoft article indicates that the /safeboot switch opens Onenote in safe mode and in that mode the user can choose to clear the cache. It does not appear to be a Onenote option to clear the cache, though perhaps Autoit could be trained to do so.
It would appear that onenote /safeboot allows you to clear the cache, but it's a manual action to be taken. I'd suggest that unless Autoit can execute the clearing then exit, then random auto-execution may be a very bad idea.
The desktop shortcut is unhelpful in locating onenote.exe. Probably the best method is by executing, from the prompt,
dir /s onenote.exxe
I found my version (2010) at c:\Program Files (x86)\Microsoft Office\Office14\onenote.exe
Under these circumstances, a batch file
#ECHO OFF
SETLOCAL
"c:\Program Files (x86)\Microsoft Office\Office14\onenote.exe" /safeboot
GOTO :EOF
can start a Onenote instance, offering the clear cache option. This may be useful during start-up, but may be useful at any time if offered as a desktop icon, for instance as the first screen it presents is the clear-cache option. You might construct an icon with the same graphic, but running the batch file, for instance...
I have a program the opens a window, reads a config file, then closes the window a fraction of a second later, then continues running in background. I want to be able to start this program one way or another without the window appearing in the first place.
Is there a way for me to launch the program (preferably on PC startup) but suppress any windows it creates?
I do not have the source code for the program in question. In that regard I am an end-user.
use a vbs script to open it:
set obj = createobject("wscript.shell")
obj.run "prog.exe",0,false
call that prog.vbs or whatever, and put it in:
"%userprofile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\startup"
Is there a way to minimize all windows automatically when launching an application?
I tried to call a .vbs file with the content above:
Set shell = wscript.CreateObject("Shell.Application")
Shell.MinimizeAll
But doing this, application is minimized too.
I'm not sure if this minimizes the windows, but it will probably satisfy your requirement:
set objShell = CreateObject("shell.application")
objShell.ToggleDesktop
This is equivalent to clicking the show desktop button.
Or as just extending your code:
shell.ToggleDesktop
Edit: Nevermind... this does the same thing as your code above. But why not just run this and then display your window? For instance, if it is your app... call the vbs and then display the window. Or if you have no control over the app, call it from a .bat file and run this vbs then your program.
What you want to do is first minimize all applications or toggle the desktop; THEN (perhaps even after a delay) open your application.
My computer has an update process that auto-restarts it around 3 am. Sometimes, upon restart, a number of .xar files are generated which Excel tries & fails to open, preventing it from executing automated processes I've set up for the morning.
How do I prevent the .xar files from being generated?
.xar files are Auto recovery files, probably created because your restart doesn't gracefully exit Excel.
You can disable auto recover on a workbook by going to the office button, excel options, save, AutoRecover exceptions for: and then checking "Disable AutoRecover for this workbook only".
If you do this for the workbooks that you're update process uses it should prevent the failure you're experiencing.