How to run a Excel spreadsheet on another computer with VBScript - excel

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.

Related

Excel VBA macro failing to save workbook on coworker's computer, but fine on mine

I'm encountering an issue trying to get some macros and VBA scripts working on a new coworker's computer. I've been using the same code for years on my machine and several others without issue, but when my new analyst tries to run it, it throws an error stating it couldn't save the file. He can manually save the file without issue, but running the script throws the error. The really weird part is that it lists the file path, but replaces the filename with a seemingly random hex byte.
On another file, if he opens it, closes with or without saving, then I open the file and try to save it using a macro, it fails saying "cannot access file...". If I copy and paste the file, open and run the macro again, it saves over the offending file no problem.
We've checked permissions and settings and haven't found anything that was different between the two systems. We're both on VMs. His hardware allowance is less than mine but otherwise they're both IT-managed and identical. I'm at my wits' end... Any advice on what may be the source of my grief would be helpful, even if not a solution.
A couple of things I'd check before trying to step through debugger mode.
Coworker does not have write access to the directory (I think you said you checked already)
The new users' Trust Center Settings are not correct
File > Options > Trust Center > Trust Center Settings... > Macro Settings > Ensure the box is checked to trust the VBA model and that macros are not disabled
The new user does not have the same libraries referenced.
Alt + F11 > Tools > References... > Check if coworkers' libs match yours
Is he using a new/different version of Excel that you? A lot of older code got bojangled when Office 365 hit the scene.
After checking settings, I would ask the following:
What error is thrown when coworker attempts to run the macro?
When does the error get thrown? It could be that you have code that is attempting to edit the file (or has another file stuck in an edit) at the same time you're trying to save it.
I would see why the file name is getting corrupted. It sounds like coworkers' machine is looking to a bit of memory or memory address that is somehow getting forgotten. Like the code is referencing a variable that is out of scope.
User Access Control has been enabled on his VM that is somehow preventing him from saving the file correctly.
Your code does not properly Quit the Excel application, so its still running in the background on coworkers' machine. Have them open/close without saving a file and check Task Manager to see if Excel is still up to confirm

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

How to store macros in one file shared among several users? (not PERSONAL.xls)

I have a 20 excel files with Automation written in VBA where the workflow is similar but there is some customizations in each of the files. I would like to have 20 files, each file per task, but I would like to put all macros into one file, with all the customizations. This will simplify fixing/changing the automation.
I want to have basically one file GlobalMacros.xls somewhere in a shared drive which contains all the modules that do not contain customizations, and only have references to it in the proper files.
Personal.xls is not an answer as I do not want users/team members to feel the changes at all.
Please tell me if there is a solution and what are downsides.
Solution 1:
My solution is to make the module in GlobalMacro.xls and then add just a reference to the module in each file. The downside is it could slow down the execution of the macros because the GlobalMacros.xls will be opened and closed in the background each time any module is called out. Also is it possible for several users using these macros at the same time?
Has anyone got any better solutions? Maybe someone had a similar problem in the past.
I'll go into a bit more detail about how to use XLAM files properly.
In a network environment, you will want to keep a local copy of the XLAM where you make changes and updates, and once all glitches are worked out, you then copy it to the server where everyone else accesses it. You will want to also make the server copy read-only, which must be reset each time you copy the file to the server. If you dont make it read-only, you will not be able to copy the updated file to the server while anyone has Excel currently open.
In addition, when the users add the Add-On to their Excel, they will be prompted to copy the XLAM file to their local computer, to which they should reply No, as they want to be able to run off the server copy so they can get your updates. I have found that most people instinctively think they should answer Yes to that question.
If they end up answering Yes, then the only way to fix it is to edit thier registry and remove the reference, and then re-add the Add-On.

Include PowerShell Script in Excel

Hoping someone here has a suggestion. I'm a network engineer, who has done a fair amount of programming/scripting over the years, but I'm not a programmer.
My current problem is, I have created a form in Excel to collect data for a particular type of install. I have also used the data entered into the form to create configurations for various network devices involved in this install. These configs are simple text which you can copy and paste, into a telnet or ssh session.
I would like to have a way to click a button in my excel form and push the config to the device automatically, so the user doesn't have to copy and paste it to the device. The sheet already includes a few buttons tied to VBScript macros which perform various functions, including dumping the configs to files. However, as VBScript lacks a native way to open a session (telnet) to a device to send/receive text, I am looking for a way to use a PowerShell script to push the configs.
I have already written a PowerShell Script to push a file containing a text config to a device via telnet, and capture any response to a log file, but I can't find a way to include the PowerShell script as a macro in Excel like you can with a VBScript in MS Visual Basic for Applications.
I've trying to avoid having to create a .MSI or .zip bundle that would include the Excel file and a PowerShell script (or writing something in a compiled .exe). The end goal of this is to have a tool that anyone with Excel running on Windows can fill in, and click a few buttons to send the configurations (text) to a device via telnet, without installing anything on their PC. This tool would be for other network engineers who know far less than me (not that I know much) about scripting or Excel.
Sub RunAndGetCmd()
strCommand = "Powershell -File ""C:\path\to\My PS File.ps1"""
Set WshShell = CreateObject("WScript.Shell")
Set WshShellExec = WshShell.Exec(strCommand)
strOutput = WshShellExec.StdOut.ReadAll
Msgbox strOutput
End Sub

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)?

Resources