Matlab to write on already open excel file

32 views (last 30 days)
Fabio Adducchio
Fabio Adducchio on 16 Jul 2018
Commented: Guillaume on 20 Jul 2018
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 Comments
Guillaume
Guillaume on 16 Jul 2018
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.

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 16 Jul 2018
"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 Comments
Guillaume
Guillaume on 18 Jul 2018
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.

Sign in to comment.


Guillaume
Guillaume on 17 Jul 2018
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 Comments
Guillaume
Guillaume on 20 Jul 2018
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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!