Running a macro that opens a MessageBox - excel

I'm invoking a macro within an Excel document via Powershell.
To invoke the macro, I have to run a named macro and call it in run. However, when the macro is invoked and completes successfully, a MessageBox will appear. As far as I know, this messagebox is the only way to find out if the process has completed successfully.
I have no control over the ability to remove the message box. The powershell script must wait for the macro to finish.
From the document: The documentation for this does not give an option for this situation, or so it appears.
Alternative Options I can work with: (But I'm not sure how to get to the point where the messagebox would be dismissed)
The Excel document can have code inserted within it via Powershell
Options can be changed within the document memory space
Is it possible to run a macro asynchronously and to check back on the execution of the macro?

There are a few convoluted ways to go about doing this.
1) Invoke another instance of the Excel application and run the Macro using that. Then how would you know whether it's done? You pass a global variable by reference to it. And use the OnTime functionality to keep checking every few seconds if its done or not.
An example for calling another excel instance is given here: Stop VBA-Script from "freezing" while sending MDX-Query
2) You can store your script as a .VBS file. Then you call the shell to run the VB script and again check some passed-by-reference variable.
3) Use a hidden worksheet as a buffer, which gets written upon in a particular location once the asynchronous code finishes running. Again, you need some clever OnTime programming to automatically run a polling service... and more importantly, to stop running it!

Related

Automate "Right-click + Print" on .xlsx files

I need to automate the act of printing .xlsx file.
I have already seen some answers to this task saying that it is possible by creating a VBA script, as well as some examples. That is not about what my question revolves around.
Thought, I know that it is also possible to right-click on a .xlsx file and click "Print", which does the exact task that I want. It opens Excel, prints the file to the default printer, then closes Excel. (Windows 7, by the way)
So I'm thinking that the work has already been done here.
What process is launched when clicking this "Print" option? Can it be launched via command line, or "clicked" by a python script or something? And if not, why? How can something so easy to click be impossible to automate? I assume a process of some sort must be launched in some way.
Found it!
This task can be easily launched using python.
import os
os.startfile('C:/path/to/the/file.xlsx','print')
This code will launch the same print task. From there, it is pretty trivial for a python developer to automate the task in his scripts.
However, if you do not know much about Python and do not want to learn it now, an easy (or lazy?) way to add it in any automation script would be to save the two lines of code above in a whatever.py file, and launch it via command line (with Python installed, of course).
The context menu print command for Office documents utilizes Dynamic Data Exchange (DDE) and does not directly run a command that can be replicated from the command line.
You can view the content of the commands in the registry. Browse to HKEY_CLASSES_ROOT\.xlsx and look at the (Default) data column. On my machine, "Excel.Sheet.12" is the type of a .xslx file. Then browse to HKEY_CLASSES_ROOT\Excel.Sheet.12\shell\ to see the commands registered for that file type. On my machine, the Print (Default) is "C:\Program Files\Microsoft Office\Office16\EXCEL.EXE" /dde and the "command" is zn=BV5!!!!4!!!!MKKSkEXCELFiles>]-z5hw$l[8QeZZR4_X=$ /dde, none of which is directly useful or accessible for running from a command line.
It will require another program to allow you to access the interface, but there are programs that allow you to make use of DDE from the command line. I recommend Freddy Vulto's Class Exec. More information and a few other similar utilities can be found here.

Automatically run data connections in XLSM

I have an XLSM file which contains 2 Web Query connection. When I go to "Connections" in the "Data" tab I am presented with the two connections I have.
For each of these I can edit some properties, one which says "Update every X minutes". I've set this to 1 minute and also ticked "Activate background update".
This, however, won't work as the web query connections aren't run anyway.
Ultimately what I need is to run these connections automatically once every hour. Preferably without any user interaction and without the document being open.
Is this possible?
You can't refresh a connection without the file being open. You can run queries on opening the file or create a VBA routine that opens the file, then uses the RefreshAll and saves over the original file every hour.
You can use VBS too.
The final solution by OP:
I ended up making a little VBS script to handle the open, refresh,
close. Then a batch script wrapper to handle running the VBS script
and logging. Finally Windows Task Scheduler to run the batch script
periodically.

Running different excel macros from one vbscript at different hours

I want to use vbs file - which will fire different macros at different hours. I was thinking about putting it in the windows startup programs folder
The point is I want this vbs to fire each of these macros in different hours (Let's assume I will have machine on long enough).
I tried to make separate vbs files for each macro and launch it with windows scheduler. It works, but i dont like this solution and I want to learn smth. To be clear what i want to achieve is:
Launching Computer at 12 -> VBScript is launching at windows start,
then it fires:
Macro a at 13, and update it on 13.15
Macro b at 15
then Macro c at 16 using data from Macro b, and then closing itself
(both macro and vbs).
OFC i will launch computer and close it manually :)
Thanks,
Dawid
You should be able to do this using the timer function which returns the number of seconds since midnight.
https://msdn.microsoft.com/en-us/library/office/gg264416.aspx
To ensure it's not checking every second try the Wait function as well.
https://msdn.microsoft.com/en-us/library/office/ff822851.aspx
I'm still new, so hopefully someone else will expand on this for you, but it should be a good starting point.

How to start Access from Commandline in regular mode

I have an update routine that involves various different procedures that runs during the night. The procedures are all stored in an excel file which calls them in the order needed using commandline.
Now I need to add an Access Database File that is to be included. I can open the Access Database and the file I want, but only in "Read only" mode. It also doesn't recognize the Macro I am calling either.
My commandline looks like this:
MSACCESS.EXE /ro "W:\Mandate.accdb" /x AutoRunProcess
Problem 1:How do you open an access file in regular mode? I have researched the topic a bit and have looked at Microsofts documentation, but found no information about how to open a database so you can run updates and save it again. The examples provided are only for opening in "read-only" mode source.
Problem 2: Macro not found Though my current code opens the correct access database file, it says it can't find the macro. However, the macro runs fine if I start it manually.
Any pointers or help appreciated.
Problem 1:
To not open the database read-only, don't specify the /ro switch. :)
Problem 2:
A general suggestion is to avoid macros as far as possible. Generally, a database should have one macro, that is AutoExec.
Although with a quick test the /x switch worked for me, even if the database has a AutoExec macro. AutoExec runs first, then the /x macro.
An alternative is the /cmd switch. You pass a string that you read in your AutoExec function with the Command() function.
Select Case Command()
Case "AutoRunProcess": Call MyProcess
Case "SomethingElse": Call AnotherFunction
Case "": ' nothing was passed in /cmd
Case Else: MsgBox "Error in command-line: " & Command()
End Select

Automatically pass a value to a script menu for automation's sake in Bash/KSH

Trying to make a small script and cron job it in order to automate a task. Said script runs another script which has already been created, grabs the output, emails to specified recipients, and cleans up the output. I've got it almost down however am running into one major issue. The script that mine is running has a menu on the outset. That is to say, running the script by itself manually, i would have to select option 1 in order to get the output i want (the only other option, 2, is quit.)
How can I automatically enter (or simulate entering) the value 1 into the other script, so it does not hang when in a cron job waiting for user input?
Is there a sane way to do this?
Thanks in Advance.
You could try something as simple as using yes | command if answering yes is all that is needed. Otherwise you probably want to use expect to drive the imaginary keyboard for you.
http://expect.sourceforge.net/
Using autoexpect to record your session is a convenient way to come up with rough draft expect scripts as well.

Resources