This App is No Longer Maintained. Visit the Replacement at StrokesPlus.net

StrokesPlus Forum
                       
StrokesPlus Forum
Home | Profile | Active Topics
Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Lua Scripts
 General Action Scripts
 Open excel files with space character in path
 Forum Locked
 Printer Friendly
Author Previous Topic Topic Next Topic  

Pierre38

France
2 Posts

Posted - 09/25/2015 :  08:15:43  Show Profile
Hello
I'm new to this great tool, so apologies if this is a basic question. I looked in the forum and I did not find an answer to my question:
I'm trying to create a script to open a file with excel.
I found how to do it, but it does not work if the file has a space character in it.
The file I'm trying to open is :
C:\Users\Pierre\Documents\Personal documents\test.xlsx

I tried :
acRunProgram("C:\\Program Files (x86)\\Microsoft Office\\Office15\\excel.exe","C:\\Users\\Pierre\\Documents\\Personal documents\\test.xlsx",0, 1)
The script seems to fail because of the space character after "personal". It does not fail at the first space characters in "Program Files (x86"

Thanks in advance for your help

Rob

USA
2615 Posts

Posted - 09/25/2015 :  08:50:38  Show Profile  Visit Rob's Homepage
To answer your question, it's because you need to put the file path in quotes. Without quotes, Excel is passed 2 parameters (each space, when not inside of quotes, indicates a new parameter). Even though you have the path in quotes for your script, that's only so the Lua engine itself knows the beginning and end of the string, but it only passes what's inside the quotes along to Windows (so it has no quotes). You'll need to wrap it in escaped quotes (\") like this (file path on my machine):
acRunProgram("C:\\Program Files (x86)\\Microsoft Office\\Office15\\excel.exe",
             "\"C:\\Users\\Rob.VOLSOL\\Documents\\Custom Office Templates\\test.xlsx\"",0, 1)
However, I'd recommend using acShellExecute instead as it's less complicated, immune to Microsoft changing the location of excel.exe, and doesn't require escaped quotes; it's the equivalent of double-clicking the file in Windows:
acShellExecute("open", "C:\\Users\\Rob.VOLSOL\\Documents\\Custom Office Templates\\test.xlsx", "", "", 1)
Note that the verb parameter ("open") is essentially (not always, though) the text you see when you right-click a file, so it can also be used to perform actions like "print", "edit", etc.
Go to Top of Page

Rob

USA
2615 Posts

Posted - 09/25/2015 :  08:57:17  Show Profile  Visit Rob's Homepage
Also, the reason the first parameter (path to Excel.exe) doesn't fail is because Windows knows that what's being passed into that parameter must only be the path to an executable (so it doesn't break on spaces)...and technically, it passes the full file path, verbatim, along to Excel, but it's standard for most programs to consider a space as the delimiter for parameters, so it's really Excel that needs the quotes around the file path.
Go to Top of Page

Pierre38

France
2 Posts

Posted - 09/25/2015 :  09:12:35  Show Profile
Excellent thank you for the ultra quick reply!

For those who would re-use the solution with acShellExecute, a small typo in Rob's answer: the " needs to be escaped, so the correct syntax seems to be:

acShellExecute("open", "\"C:\\Users\\Rob.VOLSOL\\Documents\\Custom Office Templates\\test.xlsx\"", "", "", 1)
Go to Top of Page

Rob

USA
2615 Posts

Posted - 09/25/2015 :  09:19:57  Show Profile  Visit Rob's Homepage
Hmm, it's not escaped in my script and seems to work fine. Either way, I'm glad you're up and running!
Go to Top of Page
  Previous Topic Topic Next Topic  
 Forum Locked
 Printer Friendly
Jump To:
StrokesPlus Forum © 2011-2018 Rob Yapchanyk Go To Top Of Page
Snitz Forums 2000