ActiveX object error when user switches or interacts with active Excel window

1 view (last 30 days)
I have a compiled application that uses Excel to make spreadsheets for another team to use. The connection is created like this:
try
XL = actxGetRunningServer( 'excel.application' );
catch
XL = actxserver( 'excel.application' );
end
I then create a workbook and worksheet with:
WB = XL.Workbooks.Add;
WS = WB.Worksheets;
After this I add information and color the spreadsheet as desired, adding more sheets as I go with:
WS.Add([], WS.Item(WS.Count));
Finally the workbook is saved and if I opened Excel I close it. If I used an existing Excel, I close the workbook. All works well unless the user either opens Excel prior to running the app and then activates that instance during the process or opens a new Excel once that new workbook is created. The error is MATLAB:COM:E2148189298, Error Object returned error code:0X800AC472. This happens when trying to apply a color to a cell in the workbook or sometimes when trying to create the next worksheet.
When I check within Matlab debugging the WB and WS variables seem to always maintain connection with the appropriate workbook and worksheet but somehow the calls to color and select are not working if the user selects other sheets. The exact commands causing the problems are:
range.Cells.Item(current_cell).Interior.ColorIndex = color(j, k);
For some additional background there were cases where if the user opened Excel the app would take over that copy and do everything in that active window, even though the code sets XL.Visible to off. This happened even when the XL object was created from scratch. The concern there was that the workbook could be split between the hidden XL object and the visible user created one. So the switch was made to use a running Excel if one existed.
The question is how can I safely interact with Excel such that user actions do not cause there errors?

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!