Thread Subject:
How to Use ActiveX on Existing Excel Session

Subject: How to Use ActiveX on Existing Excel Session

From: Raj Sodhi

Date: 20 Aug, 2009 17:17:21

Message: 1 of 6

Hello,

I would like to figure out how to make Matlab detect and operate on an open session of Microsoft Excel. As for right now, I'm only able to detect if there is a file that is already open. There are many situations when I forget to close my edited Excel file, so I'd like Matlab to discover and operate on my open file. Any ideas on how to do this?

===============================
xls.fname = 'Compliance Table SN267 3-4-09' ;
h = actxserver('Excel.Application');
set(h, 'Visible', 1);
% J:\Lab Data\mcm003\str10314
file = [pwd '\' xls.fname '.xls']
a = dir(pwd) ;

if ~exist(file,'file')
   ExcelWorkbook = h.workbooks.Add;
   ExcelWorkbook.SaveAs(file)
   ExcelWorkbook.Close(false);
end
 
%Open file
ExcelWorkbook = h.workbooks.Open(file);
if ExcelWorkbook.ReadOnly ~= 0
   %This means the file is probably open in another process.
   error('MATLAB:LockedFile', 'The file %s is not writable. It may be locked by another process.', file);
end
===============================

Yours

Raj

Subject: How to Use ActiveX on Existing Excel Session

From: Andy

Date: 20 Aug, 2009 17:23:21

Message: 2 of 6

"Raj Sodhi" <raj.removethispart.sodhi@skyworksinc.com> wrote in message <h6k0f1$gfo$1@fred.mathworks.com>...
> Hello,
>
> I would like to figure out how to make Matlab detect and operate on an open session of Microsoft Excel. As for right now, I'm only able to detect if there is a file that is already open. There are many situations when I forget to close my edited Excel file, so I'd like Matlab to discover and operate on my open file. Any ideas on how to do this?
>
> ===============================
> xls.fname = 'Compliance Table SN267 3-4-09' ;
> h = actxserver('Excel.Application');
> set(h, 'Visible', 1);
> % J:\Lab Data\mcm003\str10314
> file = [pwd '\' xls.fname '.xls']
> a = dir(pwd) ;
>
> if ~exist(file,'file')
> ExcelWorkbook = h.workbooks.Add;
> ExcelWorkbook.SaveAs(file)
> ExcelWorkbook.Close(false);
> end
>
> %Open file
> ExcelWorkbook = h.workbooks.Open(file);
> if ExcelWorkbook.ReadOnly ~= 0
> %This means the file is probably open in another process.
> error('MATLAB:LockedFile', 'The file %s is not writable. It may be locked by another process.', file);
> end
> ===============================
>
> Yours
>
> Raj

help actxGetRunningServer

Subject: How to Use ActiveX on Existing Excel Session

From: Raj Sodhi

Date: 2 Sep, 2009 00:32:04

Message: 3 of 6

Thanks for the pointer to actxGetRunningServer. I later learned that a lot of people had the same question. Here's a follow-up. Is there a way to discover and use an already open workbook? My work-around so far is as follows.

% ======================
try
    % use existing Excel session
    Excel = actxGetRunningServer('Excel.Application') ;
catch
    % Start Excel as ActiveX server process.
    Excel = actxserver('Excel.Application');
end

%================================
Excel.DisplayAlerts = 0; % avoids annoying dialog boxes
Excel.Visible = 1 ; % show the open Excel session.

try
    % open workbook
    ExcelWorkbook = Excel.workbooks.Open(file,0,true);
catch
    ExcelWorkbook = Excel.workbooks
    ExcelWorkbook.Close % close it and then re-open it.
    ExcelWorkbook = Excel.workbooks.Open(file,0,true);
end
%================================

Subject: How to Use ActiveX on Existing Excel Session

From: ImageAnalyst

Date: 2 Sep, 2009 01:22:26

Message: 4 of 6

It's been a while since I looked inside it, but you might want to
check out xlswrite1 on the File Exchange:
http://www.mathworks.com/matlabcentral/fileexchange/10465
Maybe it does that.

Subject: How to Use ActiveX on Existing Excel Session

From: Raj Sodhi

Date: 25 Sep, 2009 21:42:01

Message: 5 of 6

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <4d0b5094-346d-42fa-a84d-bdd9afb501c6@j19g2000vbp.googlegroups.com>...
> It's been a while since I looked inside it, but you might want to
> check out xlswrite1 on the File Exchange:
> http://www.mathworks.com/matlabcentral/fileexchange/10465
> Maybe it does that.

Hi, and thanks for the suggestion. The post you mention above only checks to see if the file exists. I did not see where it checks on whether the file is open. Did anyone else see this?

Otherwise, does anyone know how to make Matlab detect whether the Excel file is already open?

yours,

Raj

Subject: How to Use ActiveX on Existing Excel Session

From: james bejon

Date: 3 Apr, 2010 12:55:06

Message: 6 of 6

Don't know if you're still looking for a solution to this, but I think the following does the job:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/278382

The key step is 'try'-ing a .get('Workbooks', sFILE_NAME), workbooks being the application's collection of open books.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
excel activex Raj Sodhi 25 Sep, 2009 17:44:04
actxgetrunningserv... Raj Sodhi 1 Sep, 2009 20:34:06
open session Raj Sodhi 20 Aug, 2009 13:19:20
excel Raj Sodhi 20 Aug, 2009 13:19:20
activex Raj Sodhi 20 Aug, 2009 13:19:20
rssFeed for this Thread

Contact us