MATLAB 上で指定したエクセルファイルのみを閉じることはできますか?
11 views (last 30 days)
Show older comments
MathWorks Support Team
on 19 Apr 2017
Answered: MathWorks Support Team
on 19 Apr 2017
MATLAB 上でのファイル操作で、事前にexcelで同じファイルを開いた状態のときにエラーが発生することを防ぎたいと考えています。
そのため、MATLAB 上で指定したエクセルファイルのみを閉じたいと考えています。
dos('taskkill /im excel.exe')ですと、複数エクセルファイルを開いているときに一気にすべてが閉ざされてしまい、
特定のファイルのみ閉じることができませんでした。何か方法はないでしょうか?
Accepted Answer
MathWorks Support Team
on 19 Apr 2017
既に、Excelで開いた場合、actxGetRunningServer により現在実行中のンスタンスの参照を取得して利用可能です。
https://www.mathworks.com/help/matlab/ref/actxgetrunningserver.html
例:
>> h = actxGetRunningServer('Excel.Application');
>> wb = h.Workbooks;
>> awb = h.ActiveWorkbook;
>> awb.FullName
ans =
'E:\Temp\MATLAB\Book1.csv'
>> wb.Item(1).FullName
ans =
'E:\Temp\MATLAB\Book2.csv'
>> wb.Item(2).FullName
ans =
'E:\Temp\MATLAB\Book1.csv'
>> wb.Item(1).Close
% h.Quit % Excel を閉じる場合(コメントをはずして)実施
なお、AciveX API につきまして Microsoft 社の資料など、適宜ご参照ください。
0 Comments
More Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!