Matlab ActiveX error after running macros
4 views (last 30 days)
Show older comments
Hello all,
So I wrote a script that allows me to give input data from matlab to excel, run a macro (to run a process simulation software), and then take the result back to matlab. This is a repeated process for more than 50 iterations.
global ExcelApp;
ExcelApp = actxserver('Excel.Application');
ExcelApp.Visible = 1;
% OPEN THE EXCEL FILE
EO = ExcelApp.Workbooks.Open('filepath');
% EXPORT INPUT DATA TO EXCEL
Shoot = ExcelApp.ActiveWorkBook.Sheets;
Shoot.Item('Pops').Activate;
Activesheet = ExcelApp.ActiveSheet;
ActivesheetRange = get(Activesheet,'Range','D2:S18');
set(ActivesheetRange, 'Value', parT);
% RUN VBA MODULE
ExcelApp.Run('RunSimulation');
% IMPORT RESULT TO MATLAB
Shoot.Item('Result').Activate;
Activesheet = ExcelApp.ActiveSheet;
Res = get(Activesheet,'Range','B2:AH38')
out = Res.value;
But then I got the following error:
Error using Interface.000208D7_0000_0000_C000_000000000046/Item
Error in AnalysisScript (line 23)
Shoot.Item('Result').Activate;
tried to debug but nothing works so far. Sometimes the code works for 3 iterations and then the error pops up again.
Any help would be very appreciated.
Thank you!
2 Comments
Bob Feng
on 23 Feb 2017
Edited: Bob Feng
on 23 Feb 2017
Hi, Eko:
Any luck resolving this issue? I am also trying to run a MATLAB GUI that initiates an Excel macro which then calls a process simulation software (unisim). However I got stuck at the point in the macro where it is trying to create the unisim object. The macro returns the error
"Run-time Error 429: ActiveX Component Can't Create Object".
Did you have similar experience? Is there anything specific you setup on MATLAB / Excel / Process simulation software to be able to run the macro?
Thanks, Bob
Guillaume
on 24 Feb 2017
@Bob,
Eko will not get notified of your comment (only of answers). Considering the above is his only post on Answers and that he didn't even care that he got an answer, I doubt that he'll ever come back to read your comment.
Your question is not even related to his question (other than it involves activex). If you want help, start your own question.
Answers (1)
Guillaume
on 7 Oct 2016
I can think of two possible reasons for the error:
A) The sheet Result does not exist when you try to access it. As it looks like it's created by your RunSimulation macro, I would look for problems there. Note that you could obtain the names of all worksheets and check that it exists before trying to access it:
wsnames = arrayfun(@(idx) Shoot.Item(idx).Name, 1:Shoot.Count, 'UniformOutput', false);
assert(ismember('Result', wsnames), 'Result sheet does not exist')
B) Because you're relying on ActiveWorkBook and ActiveSheet, it's possible that you've activated another workbook in between. You simply clicking on another excel book would be enough to break your program.
There is absolutely no reason to use ActiveWorkbook and ActiveSheet when you already have a reference to the workbook/sheet, so I would change that anyway, even if it's not the problem because it will be a problem eventually. At the same time, I'd use better variable names than the very misleading Shoot (which is actually a Worksheets collection and EO which is a Workbook item, so:
% OPEN THE EXCEL FILE
workbook = ExcelApp.Workbooks.Open('filepath');
% EXPORT INPUT DATA TO EXCEL
worksheets = workbook.Sheets; %We already have the workbook, simply get the Worksheet collection
worksheets.Item('Pops').Range('D2:S18').Value = parT;
% RUN VBA MODULE
ExcelApp.Run('RunSimulation');
% IMPORT RESULT TO MATLAB
wsnames = arrayfun(@(idx) worksheets.Item(idx).Name, 1:worksheets.Count, 'UniformOutput', false);
assert(ismember('Result', wsnames), 'Result sheet does not exist')
out = worksheets.Item('Result').Range('B2:AH38').Value
0 Comments
See Also
Categories
Find more on ActiveX 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!