MATLAB: How to obtain a list of all currently open workbooks through an Excel ACTXSERVER object in MATLAB 7.8 (R2009a)

activeactivexMATLABobjectreferencevbaworksheetx

I am connecting to an instance of Excel using the ACTXSERVER or ACTXGETRUNNINGSERVER functions, opening a number of files, and reading data from them. Using the Excel COM API methods, I would like to be able to get a list of all Excel files that are currently open in the program.

Best Answer

Assuming you have created an ACTXSERVER or ACTXGETRUNNINGSERVER object named 'e' in MATLAB that connects to a COM/ActiveX instance of Excel:
% Opens a new server instance of Excel
e = actxserver('excel.application')
% Obtains handle to existing running instance of Excel
e = actxGetRunningServer('excel.application');
the following code demonstrates how to obtain a list of all open Excel files (i.e., workbooks) by invoking Excel's COM API methods:
% Obtain handle to collection of open Workbooks in Excel
w = e.Workbooks;
% Get number of open Workbooks
numWorkbooks = w.Count;
% Query name of each open Workbook
for i = 1:numWorkbooks
openFiles{i} = w.Item(i).Name;
end
% Display names of open Workbooks
openFiles{:}