actxGetRunningServer with multiple instances of Excel running

14 visualizaciones (últimos 30 días)
daniel
daniel el 14 de Abr. de 2016
Comentada: raym el 3 de Abr. de 2022
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

Respuesta aceptada

Guillaume
Guillaume el 14 de Abr. de 2016
"“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 comentarios
raym
raym el 3 de Abr. de 2022
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.
raym
raym el 3 de Abr. de 2022
Also, the xlsread server is in ascii, not compatible with unicode country.

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by