OpenFileInExcel
Back to ListDescription: Open a given file in Microsoft Excel, independent of default binding.
FileMaker Prototype:
Set Variable [$res; ACF_Run("OpenFileInExcel"; string_path)]
Category: EXCEL
The OpenFileInExcel Function
The OpenFileInExcel function allows you to open Microsoft Excel directly, bypassing the default application binding for file types like XML or CSV.
For instance, if you generate an Excel report as an XML file, double-clicking it might open the file in Xcode or another default app for XML files. With this function, you can ensure the file is opened in Excel automatically at the end of your report generation process.
This function is supported on both macOS and Windows and requires plugin version 1.7.2.4 or newer. For Windows, version 1.7.2.4b is recommended for an improved user experience, as it fixes an issue in earlier versions where a black command prompt window would briefly appear.
Function source:
function OpenFileInExcel ( string path )
// Location of the Excel App
string excel, launch;
if (isWindows) then
path = substitute (path, "\\", "/");
end if
string basepath = regex_replace("^(.+)/(.+\..+)$", path, "\1");
string filename = regex_replace("^(.+)/(.+\..+)$", path, "\2");
if (isWindows) then
excel = "start excel";
launch = format ("%s %s", excel,filename);
else
excel = "/Applications/Microsoft Excel.app";
launch = format ("open -a \"%s\" %s", excel,filename);
end if
print launch;
return SystemCommand ( launch, basepath);
end
Usage Example
To call the function in your script, use:
Set Variable [$res; ACF_Run("OpenFileInExcel"; string_path)]
Here, string_path is the file path of the XML or CSV file you want to open in Excel.
How It Works
The OpenFileInExcel function leverages the SystemCommand function, introduced in version 1.7.2.4 of the plugin. SystemCommand executes system-level commands and takes two parameters:
- Command: The system command to execute.
- Directory Path: The directory to change into (
cd) before running the command.
Behaviour on macOS
On macOS, the SystemCommand uses the open terminal command with the -a flag to specify Excel as the application. The resulting command looks like this:
cd "/Users/ole/Desktop";
open -a "/Applications/Microsoft Excel.app" MyXMLExcelFile.xml
Behaviour on Windows
On Windows, the function uses the start excel command with the file name as a parameter. The resulting command looks like this:
cd "C:\Users\Ole\Desktop";
start excel MyXMLExcelFile.xml
By calling OpenFileInExcel, you can ensure that your generated reports open seamlessly in Microsoft Excel, regardless of the default application settings for file types. This makes your FileMaker scripts more intuitive and user-friendly.
