Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to close Excel workbook from Matlab

1,481 views
Skip to first unread message

KtotheC

unread,
Feb 13, 2009, 1:52:01 PM2/13/09
to
I'm just trying to close an Excel workbook from Matlab. The user may have other Excel workbooks open, so I can't kill Excel completely.

I've tried using a 'tasklist' and 'taskkill' approach, but I can't get the 'taskkill' command to only kill one specific workbook, it will nuke all Excel workbooks that are open (even with a filter).

Not sure if there is a good way to use the actxserver to accomplish this?

Thanks in advance, I appreciate the help!

Vadim Teverovsky

unread,
Feb 13, 2009, 2:32:56 PM2/13/09
to
If you have the ExcelWorkbook object available, you should be able to call
Close on it.

If you don't have it available, there is almost certainly a way to look it
up from the Excel object itself. I'd suggest checking the Microsoft
documentation as the MATLAB Excel and ExcelWorkbook objects are just
wrappers around the COM objects.


"KtotheC " <kto...@nospam.com> wrote in message
news:gn4fgh$rkp$1...@fred.mathworks.com...

KtotheC

unread,
Feb 13, 2009, 2:40:04 PM2/13/09
to
Yeah, I don't have the object available, and I'm not too familiar with COM objects. I'm reading up on them now, but if you have any examples I'd be very appreciative! I have the file path of the workbook that needs to be closed, just need a set of commands to actually close it. Thanks!

"Vadim Teverovsky" <vtev...@mathworks.com> wrote in message <gn4ht9$gef$1...@fred.mathworks.com>...

Ilya Rozenfeld

unread,
Feb 13, 2009, 2:40:04 PM2/13/09
to
wbkname = 'whatever';
h = actxGetRunningServer('Excel.Application');
h.WorkBooks.Item(wbkname).Close;


"KtotheC " <kto...@nospam.com> wrote in message <gn4fgh$rkp$1...@fred.mathworks.com>...

KtotheC

unread,
Feb 13, 2009, 4:12:02 PM2/13/09
to
This does the trick, thanks!

Now, is making Excel do a 'hard' close, and not prompt the user if they want to save changes possible with this approach as well? Also, what if there are two images of Excel running? Thanks again!

"Ilya Rozenfeld" <rozeni...@alum.rpi.edu> wrote in message <gn4iak$fm4$1...@fred.mathworks.com>...

Ilya Rozenfeld

unread,
Feb 14, 2009, 12:31:01 PM2/14/09
to
h = actxGetRunningServer('Excel.Application');

% If you want to save changes
h.WorkBooks.Item(wbkname).Save

% If you want to save changes with different file name
h.WorkBooks.Item(wbkname).SaveAs(filename)

% If you don't
h.WorkBooks.Item(wbkname).Saved = 1;

h.WorkBooks.Item(wbkname).Close;

Normally you wouldn't have two instances of Excel running. If you open the instance from Matlab then you should already have a handle and don't need to use 'actxGetRunningServer'

"KtotheC " <kto...@nospam.com> wrote in message <gn4nn2$jo1$1...@fred.mathworks.com>...

KtotheC

unread,
Feb 16, 2009, 7:43:02 AM2/16/09
to
Thanks, I appreciate the help!


"Ilya Rozenfeld" <rozeni...@alum.rpi.edu> wrote in message <gn6v4l$nfr$1...@fred.mathworks.com>...

Bin

unread,
Nov 13, 2009, 12:33:18 PM11/13/09
to
I tried it but got the following error and wonder if you can give some help? Many thanks!

>> wbkname = 'whatever';
>> h = actxGetRunningServer('Excel.Application');
>> h.WorkBooks.Item(wbkname).Close;

??? Invoke Error, Dispatch Exception: Invalid index.

"Ilya Rozenfeld" <rozeni...@alum.rpi.edu> wrote in message <gn4iak$fm4$1...@fred.mathworks.com>...

Bin

unread,
Nov 13, 2009, 12:33:18 PM11/13/09
to
I tried it but got the following error and wonder if you can give some help? Many thanks!

>> wbkname = 'whatever';


>> h = actxGetRunningServer('Excel.Application');
>> h.WorkBooks.Item(wbkname).Close;

??? Invoke Error, Dispatch Exception: Invalid index.

"Ilya Rozenfeld" <rozeni...@alum.rpi.edu> wrote in message <gn4iak$fm4$1...@fred.mathworks.com>...

Paul

unread,
Jul 26, 2011, 1:57:11 PM7/26/11
to
I am having the same problem, with the error:

??? Invoke Error, Dispatch Exception: Invalid index.

I need to use Matlab to save and close an Excel file created by another process, and the above code sounds perfect, if it weren't for this error!


"Bin " <bin....@remove.this.newedgegroup.com> wrote in message <hdk58u$atj$1...@fred.mathworks.com>...

Ilya Rozenfeld

unread,
Jul 26, 2011, 4:56:08 PM7/26/11
to
What line causes the error?

Are you using Office 2007 or earlier version?

"Paul " <paul.da...@opg.com> wrote in message <j0mv5n$3a8$1...@newscl01ah.mathworks.com>...

Karan Patel

unread,
Feb 12, 2014, 2:59:08 AM2/12/14
to
@ Bin
That error came because at that time there is no such workbook already open.
If such Workbook is existing then the error won't come.

"Bin " <bin....@remove.this.newedgegroup.com> wrote in message <hdk58u$ati$1...@fred.mathworks.com>...
0 new messages