MATLAB: How to change Sheet Focus within Excel using an activex component in MATLAB 7.10 (R2010a)

activexserverexcelitemMATLABsheets

I am using an ActiveX component in GUIDE to connect to a Workbook in Excel that has three sheets. I would like to be able to programmatically change focus from Sheet1 to Sheet2 to Sheet3 as I wish.

Best Answer

Given a handle to the activex component created in guide that interacts with Excel, the following code can be used to change focus from one sheet to another:
sheet(1) = get(get(handles.activex1, 'Sheets'), 'Item',1);
sheet(2) = get(get(handles.activex1, 'Sheets'), 'Item',2);
Select(sheet(2)); %Example of selecting sheet 2

This can also be achieved using MATLAB as an ActiveXServer:
H = actxserver('Excel.Application');
H.Visible = 1;
Workbook = invoke(H.Workbooks,'Add');
w1 = get(H.Sheets,'Item',1); % first sheet
w2 = get(H.Sheets,'Item',2); % second sheet
w2.Select; %Example of selecting sheet 2