ActiveX Excel Awkward Behavior

4 views (last 30 days)
Oren Lee
Oren Lee on 28 Jan 2021
Answered: Oren Lee on 1 Feb 2021
Hi,
I am working on something for which I need to read many sheets from a single excel file. The file is on the large side, using the standard readcell(...) was too slow opening an instance of excel for each sheet. I instead ventured into using ActiveX instead, something I am only slightly familiar with.
Based on several posts I found online, I put together the following bit of code:
hExcel = actxserver('Excel.Application');
hExcel.visible = 1;
hExcel.DisplayAlerts = 0;
Wkbk = hExcel.Workbooks.Open(filePath);
for iter = 1:length(loops)
% Looping over Wkbk sheets
data = Wkbk.Worksheets.Item(sheetTitles(iter)).UsedRange.Value;
end
Wkbk.Close;
hExcel.Quit;
hExcel.delete;
The above runs properly, however there are some very strange caveats I I dont understand.
  1. The above code ONLY works if visibility is set to 1. I would prefer this run in the background without the user seeing it.
  2. Even with DisplayAlerts set to 0, I cannot close the opened Excel application without manually pressing the "Save" or "Do not Save" button on the window that appears.
Thank you guys for the help! The answers on here are always very insightful!

Accepted Answer

Oren Lee
Oren Lee on 1 Feb 2021
For those who may stumble across this in the future, I have a solution to both issues. Ultimately I belive both issues are due to internal ActiveX/Excerl settings, nothing to do with MATLAB.
First, to have the workbook not prompt the user for Save/Do not Save, the following line can be used to "trick" the workbook into thinking it was saved. I have it a line before closing the application.
Wkbk.Saved = 1;
Second, and less elegant, but achieves the same result of the user not having the Excel window bombard their screen, I use the following just before opening the workbook:
hExcel.WindowState = 'xlMinimized';

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!