how to run powershell script through excel VBA - excel

I am trying to use an Excel VBA macro to launch a PowerShell script if a certain condition is met. I'm not getting any errors in the code when it runs and I am able to manually run the PowerShell script from the Windows Explorer window without errors. However, when I execute the below VBA code, the PowerShell does not run and I am not sure why.
Dim x as Variant
If rs.RecordCount = 0 Then
x = Shell("POWERSHELL.exe " & "H:\MyFolder\MyFile.ps1", 1)
End If
I can't tell if something in the VBA code is wrong because I'm not getting any run time errors but the PowerShell script is not actually running
UPDATE: with the extra quotes I am able to see the error message now that's popping up in the cmd window but it is still having issues with the space even with the extra quotes. I have moved my script to a different file path that doesn't have spaces but I now seeing errors that running scripts are disabled. This seems like it is no longer a code based problem. thank you all!

If your file path has spaces you need to put quotes around it.
x = Shell("POWERSHELL.exe -noexit " & _
"""H:\Operations\REPORTS\Reports2018\Balance Sheet\SLmarginJE.ps1""", 1)

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

Pass argument to AppleScriptTask

When I switched to MacOS Mojave, I was no longer able to save an Excel document as a PDF to a specific location using a macro.
I have had to save it to /Library/Group Containers/UBF8T346G9.Office/⁩ and then move it to the Desktop by calling a script stored in /Library/Application Scripts/com.microsoft.Excel/.
This has worked for months. Someone who uses this macro/script combo has all of a sudden been unable to do so. No other user has a problem, some having the same combination of Excel and MacOS (16.29 and 10.14.6 respectively).
When she tries to run the macro, it throws
Invalid procedure call or argument (Error 5)
specifically on the line that calls this script.
There is another script at the beginning of the macro that does not take an argument and is working. I used MsgBox to see what string was being passed to AppleScriptTask, plugged it into the script using Script Editor, and ran it without issue.
Here is the AppleScript:
ExistsFile(sPath)
on ExistsFile(sPath)
set sPath to sPath as POSIX file
tell application "Finder" to set sPath to file sPath
set dest to (path to desktop)
move sPath to dest without replacing
end ExistsFile
Here is the call from the macro:
result = AppleScriptTask("moveToDesktop.scpt", "ExistsFile", sPath & strJobNumber & " Cover Page.pdf")
I tried the following based on what I have seen on other forums:
Set the filename to a variable and passing that to AppleScriptTask
Removing the extension in the script, removing the extension in the folder, and combinations of those two
In MacOS Mojave and newer, you'll need to do the following:
Open System Preferences - Security and Privacy - Privacy Tab - Automation - Excel ~ check off Finder.
Sounds like it could be a permissions error if it fails to run for one user only. This page has all the requirements to run the AppleScriptTask command. Possible solution could be to enable the correct permissions for the script to run using chmod command from terminal. This will provide permission for root and current user. If this doesn't work, double check that all the requirements are met on the user's system.
cd <path to script file>
sudo chmod 755 moveToDesktop.scpt
Its the spaces in the parameters you are passing to the AppleScript. You have at least one one before "Cover Letter.pdf" (and then one between "Cover" and "Letter"), probably in other cells as well that you are using to create the parameter.
I had the same problem as well as it was driving me mad that the AppleScript itself was working fine when feeding it the combined parameter directly.

How to simply run an applescript task from mac excel 2016

I am currently in the process of writing a VBA macro at work however I have run into a problem which I am hoping you can help me with.
All I am trying to do is simply run an applescript app from excel, this was very easy to do in the 2011 version but has seemingly gotten a lot more complicated in the 2016 edition.
I have researched various places online but with no luck, the only articles and guides I have come across point towards parameters and checking files but unfortunately I have no idea what any of those are. All I would like to do is to provide the filepath of the applescript so that excel can run it, is these even possible anymore?
I have attached the relevant applescript file, please don't judge me too hard for how bad it is, I only started learning last week.
The code that worked in Excel 2011 was as follows:
OSA = "/usr/bin/osascript"
SCRIPT = "/users/savePDF.scpt"
MacScript ("do shell script " & Chr(34) & OSA & " " & SCRIPT & Chr(34))
However in excel 2016 I am trying to use the AppleScript task command but get a compile error that the argument is not optional. Although I am not sure what argument they are talking about
Many thanks for all your help.
Seems like such a stupid question now I've finally figured out the answer.
Seeing as I didn't to control any variables or pass any values back and forth between applescript and VBA, the applescripttask command was pretty useless for me.
I ended up being able to launch the ".app" by simple using the hyperlink command, as follows:
ActiveWorkbook.FollowHyperlink Address:="/Users/(usernamehere)/Desktop/savePDF1.app", NewWindow:=True

Running Excel macros from Jenkins

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

Resources