Matlab to write on already open excel file

조회 수: 10 (최근 30일)
Fabio Adducchio
Fabio Adducchio 2018년 7월 16일
댓글: Guillaume 2018년 7월 20일
Good morning everyone, I'm having problems trying to tell Matlab to write on a specific file already open. It looks like it's possible only on file already open before I open Matlab. Is it correct? Because I'm looking for a code that helps me to write on a file that could be already open in my computer, so it should check if it's open and then if it is write on it, or if it's not open and then write on it.
Would you please help me out?
  댓글 수: 4
dpb
dpb 2018년 7월 16일
And the error???
Guillaume
Guillaume 2018년 7월 16일
The code given will write 1 in cell B1 of whichever workbook is currently active in Excel. This workbook may or may not be the one defined by file.
The principle of the code, to get the currently running excel instance and querying for the workbooks is correct. Using ActiveWorkbook and ActiveWorksheet is a bit iffy, but indeed we need to know what the problem is with the current code and what is actually desired.

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

답변 (2개)

Image Analyst
Image Analyst 2018년 7월 16일
"It looks like it's possible only on file already open before I open Matlab"
With actxGetRunningServer() that might be true. So use actxserver() instead of actxGetRunningServer() to launch Excel with the file NOT already open.
try
% See if there is an existing instance of Excel running.
% If Excel is NOT running, this will throw an error and send us to the catch block below.
Excel = actxGetRunningServer('Excel.Application');
% If there was no error, then we were able to connect to it.
catch
% No instance of Excel is currently running. Create a new one.
% Normally you'll get here (because Excel is not usually running when you run this).
Excel = actxserver('Excel.Application');
end
  댓글 수: 8
Image Analyst
Image Analyst 2018년 7월 18일
Yes I actually tried it myself before posting it. I opened several Excel files and had 3 Excel icons on my task bar. Then I searched for one with a particular name. It worked great. I don't know why another suggestion is needed. If you don't need fancy, precise control over Excel then maybe you can just use xlswrite() (not sure this works with an already open workbook though - it might not).
Guillaume
Guillaume 2018년 7월 18일
편집: Guillaume 2018년 7월 18일
It is possible to have completely separate instances of Excel (i.e. several Excel processes in the task manager). The older the version of Excel, the more likely it is to happen by just opening a new workbook.
It is also the default if the Application.MergeInstances property of Excel is off.
If there are several Excel processes running, actxgetRunningServer will return one of them (which one is up to the OS) and the only way to get the others is to make calls to the win32 API.
The Workbooks collection will only return Workbooks opened in the given process.

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


Guillaume
Guillaume 2018년 7월 17일
My problem is that the connection is made only with the first instance of excel that was open on my computer
First thing to check is if
%excel: excel instance returned by excel = actxGetRunningServer('Excel.Application')
excel.MergeInstances
is true, and if not, set it to true.
If that doesn't solve the problem (and the older the version of excel the less likely it will), then you're out of luck. There is no built-in way to retrieve the additional instances in matlab. The only way to do it would be with a mex file or a compiled .Net assembly.
  댓글 수: 2
Fabio Adducchio
Fabio Adducchio 2018년 7월 20일
Hi Guillame, I tried to use your code and this is what I have:
e = actxGetRunningServer('Excel.Application');
% excel: excel instance returned by excel = actxGetRunningServer('Excel.Application')
excel.MergeInstances
Undefined variable "excel" or class "excel.MergeInstances".
I think I'm missing something, my version of Matlab is 2017a, could be this the problem?
Guillaume
Guillaume 2018년 7월 20일
The first line of my code was:
%excel: excel instance returned by excel = actxGetRunningServer('Excel.Application')
If you call your variable e instead of excel, then
e.MergeInstances

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

카테고리

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