필터 지우기
필터 지우기

actxGetRunningServer with multiple instances of Excel running

조회 수: 11 (최근 30일)
daniel
daniel 2016년 4월 14일
댓글: raym 2022년 4월 3일
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

채택된 답변

Guillaume
Guillaume 2016년 4월 14일
"“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
raym
raym 2022년 4월 3일
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 2022년 4월 3일
Also, the xlsread server is in ascii, not compatible with unicode country.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by