Running Excel macros from Jenkins - excel

I am trying to run Excel macros from a Jenkins Windows service on Windows 7 64-bit VM. I have a batch file that specifies VBS files. Then in the VBS files, the Excel macros are listed. The batch files run fine locally, but when I run it from Jenkins, I get the following error:
Microsoft VBScript runtime error: ActiveX component can't create object: 'Excel.Application'
The error seems to occur on each instance of the following in the VBS files:
Set xlApp = CreateObject("Excel.Application")
Just searching around, I see that lots of other people have had problems with the version of cscript being used to execute the VBS files. On 64-bit computers, it seems that the 32-bit version of cscript must be used. But no matter how I try to force that version of cscript to be used, Jenkins seems to ignore it and display the same error, which makes me think that the cscript version is not the cause of my error.
I do have macros enabled in Excel and checked the ActiveX settings too. Like I said, double-clicking the batch file, everything works fine. There's gotta be something weird Jenkins is doing to cause the problem.
Any ideas?

Try using
Set xlApp = GetObject("Excel.Application")
On closer reading of your question, it seems like you may have trouble with executables. Hmmm, can you actually give a full command line, soo instead of run/double click foo.vbs something like script.exe foo.vba. I made up script.exe but you should get the point, why not specify the executable?
I have seldom heard of Jenkins, care to provide any documentation links you think maybe relevant to this issue. It's all useful for future stackoverflower meeting the same problem.

I use the following script to open excel and kick off a macro. I typically launch a WSF file that calls the below code via CSCRIPT from Jenkins/Batch File/Task Scheduler/etc. Works like a charm (Make sure you have Excel installed on the system this is running on.)
Set Excel = CreateObject("Excel.Application")
Set Workbook = Excel.Workbooks.Open(excelfile)
Excel.Application.Visible = True
Excel.Application.DisplayAlerts = True
Excel.Application.Run macroname
Excel.ActiveWorkbook.Close
Set Workbook = Nothing
Set Excel = Nothing

Related

I am trying to get a shortcut *.pdf file to open using command prompt via vba

I have been attempting various methods of opening a shortcut *.pdf file using command prompt via vba but have had no success yet directly from vba.
If I enter the following in cmd.exe it opens fine:
start "" /max "G:\All Production Drawings\All Production Drawings\Production Drawings\Electronic job card drawing shortcuts\920002300-1.pdf - Shortcut"
I tried the replicating this line of code in vba and had all sorts of problems with the file not either being found or various other errors so I decided to try a different method.
Shell "G:\All Production Drawings\All Production Drawings\Production Drawings\Electronic job card drawing shortcuts\920002300-1.pdf - Shortcut" - this says the file is not found. It is definetly there!
The code below runs with no errors but nothing comes up at all:
Sub opendrawing()
Dim fso As Object
Dim MyFile As String
Set fso = CreateObject("Shell.Application")
MyFile = "G:\All Production Drawings\All Production Drawings\Production Drawings\Electronic job card drawing shortcuts\920002300-1.pdf - Shortcut"
fso.Open (MyFile)
End Sub
If I use either variation of code to open the file directly it works fine but not when I try and open the shortcut. I've been looking about for a while on this one and feel it isn't possible for some reason or another.
Does anyone have any idea on this one please?
Thank you :)

Why does 'shell' opening an executable in a Visual Basic Script sometimes give a 'run time error 5' but not always?

I am playing around with the basics of VBS and have a very simple Visual Basic script which executes a file using shell.
I am using Excel under Windows 10.
Sub Whatever()
Dim Example_One As String
Example_One = "C:\Users\IEUser\python-3.8.0.exe"
Shell Example_One, vbNormalFocus
End Sub
The weird thing is that some files will run (e.g. Python as above) while others e.g. VLC, WinRAR give the error: Run time error 5: Invalid procedure call or argument.
I simply cannot figure out why some .exe files will run and others will not. I thought it might be something to do with how the installation works but ChromeSetup.exe works while VLC.exe does not (even though they both ask "Do you want this app to make changes to your device?" (Python has it's own installer). Double-clicking all files in Windows Explorer runs them as normal (they don't ask for any special rights).

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

Run-time Error '1004' - Method 'Open' of object 'Workbooks' failed

OK, so far I've uninstalled & re installed Office-2010 3 to 4 times, done hours of research for 3 days with no success. I started getting this error either from Vbscript or Excel VBA, performing the same command that has worked for months. I am not sure what I may have changed, I don't recall changing anything that could cause it but I nailed it down to the ReadOnly:=True parameter. I think something may be wrong with the registry... but re-installing should fix it right? It didn't...
Anyways, very simple command that I recorded on Excel 2010. This command WORKS FINE and the file opens:
Sub Macro1()
Workbooks.Open Filename:="C:\temp\file_9928_131101.xlsx"
End Sub
But when I add the ReadOnly:=True parameter, it does NOT WORK:
Sub Macro1()
Workbooks.Open Filename:="C:\temp\file_9928_131101.xlsx", ReadOnly:=True
End Sub
This is the returned error Run-time error '1004' Method 'Open' of object 'Workbooks' failed:
When I click Debug, the error is at the only line of code.
The file is corrupted. Resave it with another name and change the name in the function. Try that it works and after that rename the file as you want to call it.
It worked for me and I had a corrupted file. The read only shouldn't be a problem.
Putting an answer here for others like myself who have this issue and the normal solutions don't work.
Another potential cause of this is corrupted temporary files. I think it may only apply if the file you are trying to open is on a network drive or other remote host.
Anyway, try wiping your temp folder (as in the one you get to if you type %temp% into Windows Explorer) then restarting the computer.
I have had the same issue with an Access file stored in a local OneDrive folder, referencing an Excel file stored in the same local OneDrive folder. The solution was to move all files into a "static" (i.e., not synchronized, not OneDrive) folder.
Thought that this specific case/application might help someone.
I realise this is late but if you want to open & repair a corrupted workbook automatically use:
Set oWB = Workbooks.Open(Filename:="C:\my\file\path.xlsx", CorruptLoad:=XlCorruptLoad.xlRepairFile)
If you are downloading the file from some external source (eg- email), just open the file directly from the email and then save the file. Try opening the file in macro ...hope it works...it worked for me....:)
I had a rogue excel process that was running in the background. When I killed it from the task manager the code worked. I hope this helps.
I was too, going crazy with Workbook.Open function, getting the weird Open fail error for no obvious reason. My code also did some some copy-pasting after Open function, which seemed to trigger it.
Eventually I found out that turning ScreenUpdating OFF seemed to cause this - keeping ScreenUpdating ON, made things click for me (maybe someone finds this helpful in the future). I'm one of those who tends to switch ScreenUpdating off for all code whenever possible.
Application.ScreenUpdating = False
I know I'm answering late on this, but I resolved a similar issue (same error but running excel from a .Net app) by making sure VBA was installed correctly on the target machine.
Control Panel->Programs and Features->Uninstall a Program... find your Office install, Right-click and select "change" ->Add Remove Features->Office Shared Features->Visual Basic for Applications->Run From My Computer
This did it for me.

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