actxGetRunningServer with multiple instances of Excel running

Hi Guys, I'm having some issues stabilizing my 2500+ lines of script, during which I repeatedly write/read to/from excel using a combination of actxserver, xlsread, xlswrite, fopen and close operations. My script crashes at random times during these operations, it doesn’t every time but it's consistent. I have narrowed the issue down to mainly actxserver, xlsread and xlswrite. The problem is multiple instances of excel running, some of which are open manually outside of MATLAB and some within. Writing to existing, same filename, open excel workbooks using xlswrite contributes to crashing. I have seen member contributions of custom xlsread/write functions which attempt to leverage already running activex servers and/or by extracting the server calls from xlsread/write. These solutions are ok but this will involve heavy script edits on my side as well as introduce non MATLAB functions which I need to package with my script on distribution, which I would like to avoid. MATLAB should be enough. I think this may be the only solution.
Regarding actxGetRunningServer, from testing this function can’t see multiple instances of excel running, just the first one started and its workbooks. If this function could see them all then I could leverage it to mitigate issue stated above. The MATLAB help file says “When multiple instances of the Automation server are running, the operating system controls the behavior of this function.” What does this mean exactly? Does this confirm my findings that in fact it’s impossible to access multiple instances of excel? Or is there another way?
Also, where is the code for actxGetRunningServer.m? when I hit edit actxGetRunningServer all I get is the description.
Thanks Daniel

 Accepted Answer

"“When multiple instances of the Automation server are running, the operating system controls the behavior of this function.” I'm not 100% sure, the fine details of COM servers are complex and it's been a while since I've dealt with it, but it's down to the OS (Windows) to manage COM instances. So if several instances are running, it's up to the OS to connect you to one of them. For the purpose of COM you shouldn't care which one it is.
Note that even if you have several Excel windows open, that does not mean that there are several COM (= activex) servers running, particularly with newer versions of excel that go out of their way to prevent this.
In any case, the number of open instances of excel shouldn't matter to your code. What do you mean by crash? Matlab is forced to shutdown by windows, or your code stop executing with an error? You can recover from the latter by wrapping the offending line(s) in a try ... catch block and retrying the operation (if the issue is I/O).
the code for actxGetRunningServer is compiled into matlab, you won't be able to see it (it's written in C or other low-level language anyway). Plenty of built-in functions are compiled into matlab. You still get a .m file so that help functionname can display something.

7 Comments

Hi Guillaume, thanks for the response. Yeah the problem is xlsread/write, xlsfinfo all start their own instance of excel, a separate server yes, then close them when done, I know because I can see them in task manager. I think the problem is, since I'm calling these functions in rapid succession excel can't keep up with opening and closing on time resulting in a breakdown in communication which leaves instances hanging/not closing, which results in my scrip hanging stuck, in till I go and manually kill the tasks from task manager.
I implemented a solution but unfortunately there was no way to do it without modified these built in MATLAB function, including actxserver. I renamed them so I can keep the old ones and use those in my script instead, problem solved. I essentially check to see if there is an instance already running and attempt to use it, instead of just opening a new one like xlsread/write and xlsfinfo does. Problem is now I have to package those functions and instruct users to plop them in ../winfun and ../iofun.
Thanks again for your help.
by the way, how could I get access to compiled MATLAB functions like actxGetRunningServer? For learning, I don't have a problem reading lower lever code.
Thanks Daniel
If you've overriden xlsread, xlswrite, etc., you don't need to put the modified files where the originals reside. You can put them in the same folder as your code (or even better in a folder called private within the same folder as your code) and they'll take precedence over the matlab's one.
Probably, the only way you can get access to the source code of actxGetRunningServer is if you work for Mathworks. It's their intellectual property, I doubt they'll give it away.
If you want to learn COM/ActiveX, reference is here.
Hi Guillaume, thanks for your feedback. I'm glad you brought this up because that's exactly what I tried and it didn’t work. I don't want to add functions to MATLAB directories. See error below, problem is, those functions call other built in functions and MATLAB can't find them unless I put them inside the original MATLAB directories.
Undefined function 'validpath' for input arguments of type 'char'.
Error in xlsfinfo (line 44) filename = validpath(filename);
How do I fix the pathing issue? I tried creating a private folder as you mentioned, still same error. I think I may have to pull out those functions as well and put them in my private folder.
Thanks for all your help DAniel
Indeed, if your replacement xls** is a modified version of the original, you also need a copy of its private dependencies. The simplest way may indeed be to copy the toolbox\matlab\iofun\private folder into your own private folder.
It worked!, nice one Guill. I have like 50 functions in my private folder but I guess that's ok, it was inevitable.
Thanks again Guillaume!
Daniel
I noted that xlsread will create a hidden and never-dying special server that always has priority when actxGetRunningServer is called.
So this cause a probem that no matter how many servers I re-create, the actxGetRunningServer is always connected to the hidden xlsread-generated server, which never dies even when I change the raw code by adding a Excel.Quit() to the private file of Mathwork.
Also, the xlsread server is in ascii, not compatible with unicode country.

Sign in to comment.

More Answers (0)

Asked:

on 14 Apr 2016

Commented:

on 3 Apr 2022

Community Treasure Hunt

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

Start Hunting!