Running different excel macros from one vbscript at different hours - excel

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.

Related

How to call macros using azure api

I have an excel file containing around 100K rows in onedrive. This file contains some macros. I want to run these macros using api.I want to pass these macro functions as parameters.
For example
https://graph.microsoft.com/beta/me/drive/root:/book1.xlsx:/workbook/tables/{id|name}
Here, I want to call macros function in place tables/{id|name}
Any way to do this?
Thanks
To be able to run an Excel macro, you need a running Excel instance that opens the file containing the macro and runs the code. There is no way around this I'm afraid.
So you need a machine - physical or virtual - where Excel is installed. Then you can have a script open the Excel file there and run the macros.
If you are working in a cloud environment or on a web server for example, you would be better off rewriting the code in another language, independent of Excel. (Trust me, I've been there, done that.)

Setting breakpoints on all lines of VBA code

I need to test a very long program that takes many decisions based on live data from an online database. So it is unfeasible to use step in (F8) for an entire run of the program.
I was wondering if there was a way of setting a breakpoint on EVERY line of code of the project. Then when I have tested that line I can clear it and keep running. At the end I will know what part of the program was not tested because it will still have break points.
Any ideas on how to SET EVERY LINE OF A VBA PROJECT TO A BREAKPOINT?
This is not a feature of the VBA Editor. Instead use "Watches"

Running a macro that opens a MessageBox

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!

How to create excel file by copying CSV files and running VBA macros remotely from Linux

I'm running some task on Linux which produces certain CSVs. Once they’re produced, I need to copy them into different sheets of an excel file and then run some VBA macros to generate a report. Since this has to be done multiple times a day (with no pre-defined/fixed schedule), I’d like to automate it.
Here is what I tried:
I wrote a VBScript, which takes the CSV files and other required input files, opens an excel file, copies the CSVs into the excel, runs some VBA macros, and saves and closes the result. This VBScript works fine when triggered locally on a windows server.
I have uploaded a simpler version of the files involved here which somewhat clarify my requirements (Note that the macros I need to run are much more complicated (~500 lines) compared with the macros here).
There are four files inside the archive:
Template.xlsm: The Excel template of the report into which data is to be pasted
Input_Sheet2.csv : The input data to be pasted into the template into sheet 2
Input_Sheet3.csv : The input data to be pasted into the template into sheet 3
Run.vbs: The vbscript which triggers the “copy-paste” macro present inside the Excel template.
The command to run the VBScript is: ‘cscript //Nologo Run.vbs /NoCancel’
Now, in order to automate whole task, I thought to trigger this VBScript remotely from Linux over ssh. I installed the cygwin ssh daemon on the windows box and facilitated ssh connections from the Linux machine. This cygwin ssh connection from Linux allows to run several windows commands, but fails to run the VBScript and doesn't produce any excel output.
I can't figure out which component has limitations. Is it cygwin ssh not facilitating execution of VBScript? Is it VBScript not facilitating creation of files remotely? Is it ssh client on Linux not allowing opening of excel in GUI format? Or there is another way to automate this task from Linux.
Thanks,
Shubham
I'd focus on:
Did you test running your script from the Cygwin Terminal? (I did try a simple script, that opened Execl (.Visible = True) successfully, so I believe your fears concerning cygwin not allowing to open the Excel GUI are unnecessary).
Do the sshd user account's path and cwd allow the short invocation "cscript Run.vbs" or should the long version "/cygdrive/c/WINDOWS/system32/cscript.exe /home/user/Run.vbs" be used?
Does the sshd user have access/permissions to all involved files and folders?
You didn't forget the unix file systems are case sensitive? (I did and wasted some time)?

How to run a Excel spreadsheet on another computer with VBScript

I have the following situation:
There is a Excel workbook saved on another computer on my network. I need to execute the file on that computer, because it is connected with a Big Screen to show some data. I was trying to automate the execution of the workbook but all that I can get is to make it run on MY computer, and I need it to run on THAT computer
The relevant part of the vbs code that I am using is the following:
Set objXL = CreateObject("Excel.Application")
objXL.Visible = True
Set objXLWorkbook = objXL.Workbooks.Open(path)
I know that this method can only open Workbooks on my computer. Anyone knows how to make it run Excel on another computer?
You cannot - out of the box - run programs or scripts on remote computers. Otherwise, it would be even less difficult than it is today to install a virus remotely. Your vbs could simply install and activate a virus instead of an excel file.
Nonetheless its quite a common thing that one wants to do every now and again (the remote task, not the virus - especially administrators love to scare users by remotely opening some funny stuff).
Have a look at an article from Bill Stewart where he explains how to use Powershell to accomplish your task.
The required setup on the remote computer still needs to be done once.

Resources