Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to close Excel workbook from Matlab

Subject: How to close Excel workbook from Matlab

From: KtotheC

Date: 13 Feb, 2009 18:52:01

Message: 1 of 12

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!

Subject: How to close Excel workbook from Matlab

From: Vadim Teverovsky

Date: 13 Feb, 2009 19:32:56

Message: 2 of 12

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 " <ktothec@nospam.com> wrote in message
news:gn4fgh$rkp$1@fred.mathworks.com...
> 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!

Subject: How to close Excel workbook from Matlab

From: Ilya Rozenfeld

Date: 13 Feb, 2009 19:40:04

Message: 3 of 12

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


"KtotheC " <ktothec@nospam.com> wrote in message <gn4fgh$rkp$1@fred.mathworks.com>...
> 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!

Subject: How to close Excel workbook from Matlab

From: KtotheC

Date: 13 Feb, 2009 19:40:04

Message: 4 of 12

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" <vteverov@mathworks.com> wrote in message <gn4ht9$gef$1@fred.mathworks.com>...
> 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 " <ktothec@nospam.com> wrote in message
> news:gn4fgh$rkp$1@fred.mathworks.com...
> > 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!
>

Subject: How to close Excel workbook from Matlab

From: KtotheC

Date: 13 Feb, 2009 21:12:02

Message: 5 of 12

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.nospam@alum.rpi.edu> wrote in message <gn4iak$fm4$1@fred.mathworks.com>...
> wbkname = 'whatever';
> h = actxGetRunningServer('Excel.Application');
> h.WorkBooks.Item(wbkname).Close;

Subject: How to close Excel workbook from Matlab

From: Ilya Rozenfeld

Date: 14 Feb, 2009 17:31:01

Message: 6 of 12

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 " <ktothec@nospam.com> wrote in message <gn4nn2$jo1$1@fred.mathworks.com>...
> 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.nospam@alum.rpi.edu> wrote in message <gn4iak$fm4$1@fred.mathworks.com>...
> > wbkname = 'whatever';
> > h = actxGetRunningServer('Excel.Application');
> > h.WorkBooks.Item(wbkname).Close;

Subject: How to close Excel workbook from Matlab

From: KtotheC

Date: 16 Feb, 2009 12:43:02

Message: 7 of 12

Thanks, I appreciate the help!


"Ilya Rozenfeld" <rozeni.nospam@alum.rpi.edu> wrote in message <gn6v4l$nfr$1@fred.mathworks.com>...
> 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'
>

Subject: How to close Excel workbook from Matlab

From: Bin

Date: 13 Nov, 2009 17:33:18

Message: 8 of 12

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.nospam@alum.rpi.edu> wrote in message <gn4iak$fm4$1@fred.mathworks.com>...
> wbkname = 'whatever';
> h = actxGetRunningServer('Excel.Application');
> h.WorkBooks.Item(wbkname).Close;
>
>
> "KtotheC " <ktothec@nospam.com> wrote in message <gn4fgh$rkp$1@fred.mathworks.com>...
> > 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!

Subject: How to close Excel workbook from Matlab

From: Bin

Date: 13 Nov, 2009 17:33:18

Message: 9 of 12

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.nospam@alum.rpi.edu> wrote in message <gn4iak$fm4$1@fred.mathworks.com>...
> wbkname = 'whatever';
> h = actxGetRunningServer('Excel.Application');
> h.WorkBooks.Item(wbkname).Close;
>
>
> "KtotheC " <ktothec@nospam.com> wrote in message <gn4fgh$rkp$1@fred.mathworks.com>...
> > 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!

Subject: How to close Excel workbook from Matlab

From: Paul

Date: 26 Jul, 2011 17:57:11

Message: 10 of 12

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.chen@remove.this.newedgegroup.com> wrote in message <hdk58u$atj$1@fred.mathworks.com>...
> 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.nospam@alum.rpi.edu> wrote in message <gn4iak$fm4$1@fred.mathworks.com>...
> > wbkname = 'whatever';
> > h = actxGetRunningServer('Excel.Application');
> > h.WorkBooks.Item(wbkname).Close;
> >
> >
> > "KtotheC " <ktothec@nospam.com> wrote in message <gn4fgh$rkp$1@fred.mathworks.com>...
> > > 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!

Subject: How to close Excel workbook from Matlab

From: Ilya Rozenfeld

Date: 26 Jul, 2011 20:56:08

Message: 11 of 12

What line causes the error?

Are you using Office 2007 or earlier version?

"Paul " <paul.dabrowski@opg.com> wrote in message <j0mv5n$3a8$1@newscl01ah.mathworks.com>...
> 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.chen@remove.this.newedgegroup.com> wrote in message <hdk58u$atj$1@fred.mathworks.com>...
> > 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.nospam@alum.rpi.edu> wrote in message <gn4iak$fm4$1@fred.mathworks.com>...
> > > wbkname = 'whatever';
> > > h = actxGetRunningServer('Excel.Application');
> > > h.WorkBooks.Item(wbkname).Close;
> > >
> > >
> > > "KtotheC " <ktothec@nospam.com> wrote in message <gn4fgh$rkp$1@fred.mathworks.com>...
> > > > 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!

Subject: How to close Excel workbook from Matlab

From: Karan Patel

Date: 12 Feb, 2014 07:59:08

Message: 12 of 12

@ 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.chen@remove.this.newedgegroup.com> wrote in message <hdk58u$ati$1@fred.mathworks.com>...
> 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.nospam@alum.rpi.edu> wrote in message <gn4iak$fm4$1@fred.mathworks.com>...
> > wbkname = 'whatever';
> > h = actxGetRunningServer('Excel.Application');
> > h.WorkBooks.Item(wbkname).Close;
> >
> >
> > "KtotheC " <ktothec@nospam.com> wrote in message <gn4fgh$rkp$1@fred.mathworks.com>...
> > > 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!

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us