unable to access Excel with ActiveX when compiled as a com object

3 views (last 30 days)
When compiling as an Excel Addin (or COM) the exposed function is 'TestFunction' which calls 'GetOpenExcelFile' which in turn should return a file handle on an open Excel file. The program works in the Matlab environment, and when compiled as a standalone Matlab .exe but not when compiled as Addin for Excel or COM. When called through the VB wrapper the code hangs at the point that 'GetOpenExcelFile' is called. Any help appreciated. Both routines are pasted below.
function [ output ] = TestFunction( cellValue ) %TestFunction returns binary succes/failure on calling GetOpenExcelFile(cellValue)
diary('C:\Users\Robert\OneDrive\Documents\MatLab\ExcelTest\log.txt');
diary on;
try
display('TestFunction: try');
%output=cellValue*2;
display(cellValue);
e=GetOpenExcelFile(cellValue);
%output=true;
catch
display('TestFunction: catch');
output = false;
end
display('TestFunction: end');
diary off;
end
function [ e ] = GetOpenExcelFile( range ) %Returns a handle to an open Excel file % range is not used in this function try display('GetOpenExcelFile: start'); try display('GetOpenExcelFile: Get open file'); drawnow; pause(0.05); % use existing Excel session e = actxGetRunningServer('Excel.Application') ; e.DisplayAlerts = 0; % avoids annoying dialog boxes e.Visible = 1 ; % show the open Excel session. e.Workbooks.Item('Test1.xls').Activate; display('GetOpenExcelFile: file open and ready'); drawnow; pause(0.05); catch existingExcelError display('GetOpenExcelFile: 1st catch - cant find open'); drawnow; pause(0.05); % Start Excel as ActiveX server process. e = actxserver('Excel.Application'); e.workbooks.Open('C:\Users\Robert\OneDrive\Documents\MatLab\ExcelTest\Test1.xls',0,true); e.DisplayAlerts = 0; % avoids annoying dialog boxes e.Visible = 1 ; % show the open Excel session. e.Workbooks.Item('Test1.xls').Activate; display('GetOpenExcelFile: 1st catch - openend from file'); drawnow; pause(0.05); throw(existingExcelError); end catch display('GetOpenExcelFile: second catch - couldnt open from file'); drawnow; pause(0.05); end display('GetOpenExcelFile: exit'); drawnow; pause(0.05); end
Thank you
Rob

Answers (0)

Community Treasure Hunt

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

Start Hunting!