'Excel : Get the name of the file

I want to retrieve the name of the file opened by the user knowing that the Excel file was not opened by my Delphi application.
I tried :

EXCEL := TEXCELApplication.Create(Self);
Edit2.Text := EXCEL.ActiveWorkbook.FullName[lcid];

But this does not retrieve the file name with its path (File Path).

Do you have any idea how to do this?
Thank you



Solution 1:[1]

Microsoft documentation says that “Workbook.FullName property Returns the name of the object, including its path on disk”. I checked it on current version of excel – all works fine, maybe problem in older version? Anyway, there is other property to get filePath, you can combine it.

var EXCEL := TEXCELApplication.Create(Self);
try
EXCEL.Connect;
var FilePath := EXCEL.ActiveWorkbook.Path[0];       //C:\Users\user\Documents
var FileFullName := EXCEL.ActiveWorkbook.FullName[0]; //C:\Users\user\Documents\Auto.xlsx
ShowMessage(FilePath + sLineBreak + FileFullName);

EXCEL.Disconnect;
finally
excel.free;
end;

If FilePath is empty string – then file is not saved yet.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1