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
Related
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)
Recently a large number of security updates were pushed for Microsoft Office at work. We have an Excel tool with a Visual Basic backbone which worked prior to the updates, and now does not work.
I have done some isolated testing and have found that we see the same 'error 75' when using the FileCopy method on .BAT and .EXE files. Other less 'risky' file types seem to be ok. This is also an issue with the 'Open' command.
Does anybody know of a workaround for this issue when copy/pasting .EXE and .BAT files, or maybe this is a known issue with a certain security update? I have searched everywhere, but the Google is failing me. I am hoping either:
Someone knows which security update is the culprit and I can try
uninstalling it to see if that fixes the issue
There is a different way to copy/open/manipulate .BAT and .EXE files that doesn't violate the new security patch.
Some background:
We are using Microsoft Office Professional Plus 2010 and the tool uses Excel as the interface.
Here is the testing code I am using. I have verified that .TXT and .DAT files run just fine while .EXE and .BAT files cause 'error 75' indicating a permissions error. And before you ask, yes, the file path is correct, I have just omitted my employee identification information.
Sub Macro1()
' Declare variables
Dim Filename, SourcePath, DestPath As String
' Pathname variables
Filename = "test.dat"
SourcePath = "C:\Users\<REDACTED>\Desktop\working_copy\" & Filename
DestPath = "C:\Users\<REDACTED>\Desktop\working_copy\test\" & Filename
' Copy file from Source and paste in Destination
FileCopy SourcePath, DestPath
End Sub
Here is an image with the updates that were installed. The tool worked prior to the 6/14/2017 dump, so anything before can be disregarded.
Sorry for the image, too many to enter them manually, no easy way to copy from the update list:
MS Office 2010 Security Updates
Nothing with the tool or file structure changed. The only thing different is the updates. Also, we tested the same tool with Microsoft Office Home 2016 and it worked without a hitch.
Any and all help will be appreciated. Thanks in advance!
While I never found a real fix for this issue, I did come up with a slightly 'hacky' work-around.
The MS security patch prevented me from copy/pasting .BAT and .EXE files, I but could still save .BAT files.
So, I ended up having my script create a text file called "copyExe.txt" which had a single line:
ECHO F|xcopy <source> <destination>
This was then renamed to be "copyExe.bat" and run.
By creating a .txt file, I circumvented the errors when using the VBA Open function on .BAT files (so I could actually create this new batch file). This batch file has the sole purpose of copy/pasting a .EXE file from one folder to another, which circumvented errors when trying to copy/paste .EXE files.
After the copy/paste script is run, it is deleted.
All-in-all it feels like a very silly solution, but it works and is very non-invasive for the user.
I am trying to create standalone app from a script with xlwings. I used both py2exe and cx_freeze. Both work just fine outside Excel (I added some tests). When I import either of them as a VBA macros in Excel, they also work fine. But so far, on my computer only. I asked my friend to check if the script works on her computer and she got the following error:
So I thought it make sense to figure out whether I do something wrong. I sent her a Fibonacci example and it raised sort of the same error:
It should be something wrong with names of the files, but it's not like obvious for me what exactly. Maybe somebody already knows how to deal with it? Thanks!
If files are downloaded from the internet, they only work right away in the first instance of Excel. Meaning: Your friend should try to close Excel completely. Also check in the Task Manager if there is still an EXCEL process running and if so, kill it. Then try again in a fresh instance of Excel.
update:
Ah I see - the problem seems to be a bug happening when there are unicode characters involved in the file path! I've opened a issue here. In the meantime, try to run the file on a path without unicode characters.
I'm porting some VBA from Excel 2003 (Windows XP) to Excel 2004 (OS X). Excel 2004 contains a VBA interpreter that allows you to fob off some commands onto Applescript using the MacScript() function.
This is all well and good for what I want to do (which is to launch files in their default application, something which on the Windows version uses an API call and is obviously incompatible with the Mac version). The code I'm using to generate the Applescript is as such:
sScript = "set filepath to POSIX path of """ & path & """" & Chr(13)
sScript = sScript & "try" & Chr(13)
sScript = sScript & "set command to ""open "" & quoted form of filepath" & Chr(13)
sScript = sScript & "do shell script command" & Chr(13)
sScript = sScript & "end try"
MacScript(sScript)
Now everything I've done and read tells me that this ought to work. It works when I run it from the Applescript Editor. And yet, it always fails, no matter what. And what is clearly failing is the "POSIX path" part. It throws a generic Error 5, "Invalid procedure call or argument," which isn't very enlightening.
This seems rather essential to its operation, and I've found zero evidence that other people have had this trouble before me. It's driving me mad. "POSIX path" seems a standard part of the Applescript definition, yet it always fails here. (It converts the file from Macintosh HD:Users:MyUserName:Blah to /Users/MyUserName/Blah.)
Does anyone have any suggestions? I'm thinking that I'll just need to write my own POSIX path function in VBA to make it work with this code, if the MacScript() function refuses to do it on its own. But I'm wary about parsing strings and etc. regarding a function I only halfway understand (the above case is easy, obviously, but I don't know how complicated it could get).
You don't need to do this using a shell command and can then avoid using "posix path". Try this...
tell application "Finder" to open file path
I want to write a bash script for a mac which takes a text file containing a table of numbers (can be .csv), converts the numbers to a chart, saves the chart (any file format that I can display on my web page), and exits. It must do this unattended. No user interaction.
I know bash, perl, and a little AppleScript, and I can learn VBA or whatever else. I am about to plunk down money for the new Excel 2011 for Mac. I think I want Excel rather than Numbers'09, but I am not at all sure, and I think VBA or AppleScript will allow me to automate most of the work.
Is AppleScript + Excel a good choice? If so, then I'm home free. Applescript programs can be launched from bash via osascript command.
But maybe VBA is better? But how can I launch a Excel VBA script from bash on a mac?
Summary of questions: (1) AppleScript or VBA or something else? (2) Excel or Numbers? (3) How to launch Excel VBA script from bash?
Thanks in advance,
Ken
Don't forget to try OpenOffice (and derivatives). It's been a while since I last worked with it, but they have a macro language and some VBA support (don't know how good it is now, I'm sure it's progressed) - so it could likely generate your graphs for you.
Alternatively, this sounds like something Gnuplot could very easily do. See this article where they start with the raw data in a text file, and create a PNG file for use in a web page: http://www.ibm.com/developerworks/aix/library/au-gnuplot/index.html
Just thought you should know about other options than Excel and Numbers...
You should definitively consider using gnuplot (free), which can easily create graphics and even HTML5 canvas elements from different input file formats via the command line.
iWork 09 does support Applescript. Pages is pretty good with its support. Numbers far less so. I think most expect more expansive Applescript support with the new version as Numbers only has partial support. So for what the original question asks you really need to use Excel.
If you are going to script I'd look seriously at Python or Ruby combined with Appscript rather than Applescript or VBA. Applescript is a bear of a language to write in and using one of the other languages gives you all the libraries of that language. That allows quite a bit more powerful code to be written.
If you are just writing for Office and don't need much other functionality then I'd probably stick with VBA.
You might like to take a look at Google Chart Tools.
It is exceptionally useful for including charts and graphs within web pages.
You just create a URL containing the data as a src for an image, then tada, it appears.
Have a play.
For instance, a few parameters:
chd=t:10,20,30
chco=FF0000|00FF00|0000FF
chs=320x240
cht=p3
chdl=Red|Green|Blue
Makes a URL: https://chart.googleapis.com/chart?chd=t:10,20,30&chco=FF0000|00FF00|0000FF&chs=320x240&cht=p3&chdl=Red|Green|Blue
Which forms a chart:
I would recommend using Numbers and AppleScript if you are on OS X—these are well integrated. Also, you could use Automator to make your life even simpler.
Microsoft removed VBA support from Office with v2008. iWork '08 has no Applescript support, and while I can't speak directly about v'09 given Apple's recent decline in scriptable applications I wouldn't count on it.
The sure, long-term bet here would be to use Excel with Applescript.