How can I determine if an XLS-file is open in Microsoft Excel, without using DDE commands, using MATLAB 7.7 (R2008b)?
조회 수: 4 (최근 30일)
이전 댓글 표시
MathWorks Support Team
2009년 6월 27일
편집: MathWorks Support Team
2016년 4월 25일
I want to check whether an XLS-file is currently open in Microsoft Excel. In the past I used the command:
check_open = ddeinit('Excel',excelfile);
Now, DDEINIT has been deprecated. Is there any other workaround to resolve this issue?
채택된 답변
MathWorks Support Team
2016년 4월 25일
To check if an XLS-file is open in Microsoft Excel, you may use any of the alternatives below:
1. Using 'ActiveX' commands:
try
%Check if an Excel server is running
ex = actxGetRunningServer('Excel.Application');
catch ME
disp(ME.message)
end
if exist('ex','var')
%Get the names of all open Excel files
wbs = ex.Workbooks;
%List the entire path of all excel workbooks that are currently open
for i = 1:wbs.Count
wbs.Item(i).FullName
end
end
The code above will list all the open .xlsx file.
2. Using 'FOPEN':
[fid msg] = fopen('path\to\excel\file\filename.xls','a');
if fid==-1
disp('The file is already open.')
else
fclose(fid);
disp('If the file did not already exist, it has been created.')
end
In the above code, 'fid' is returned as '-1' if MATLAB was unable to open the file (since file is open in another application). Please note that an empty file will be created if it did not exist in the location specified to FOPEN .
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!