MATLAB >> How to close Excel workbook from Matlab

by KtotheC » Sat, 14 Feb 2009 02:52:01 GMT

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!

MATLAB >> How to close Excel workbook from Matlab

by Vadim Teverovsky » Sat, 14 Feb 2009 03:32:56 GMT


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.

MATLAB >> How to close Excel workbook from Matlab

by KtotheC » Sat, 14 Feb 2009 03:40:04 GMT

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!

MATLAB >> How to close Excel workbook from Matlab

by Ilya Rozenfeld » Sat, 14 Feb 2009 03:40:04 GMT

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

MATLAB >> How to close Excel workbook from Matlab

by KtotheC » Sat, 14 Feb 2009 05:12:02 GMT

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!

MATLAB >> How to close Excel workbook from Matlab

by Ilya Rozenfeld » Sun, 15 Feb 2009 01:31:01 GMT

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'

MATLAB >> How to close Excel workbook from Matlab

by KtotheC » Mon, 16 Feb 2009 20:43:02 GMT

Thanks, I appreciate the help!

MATLAB >> How to close Excel workbook from Matlab

by Bin » Sat, 14 Nov 2009 01:33:18 GMT

I tried it but got the following error and wonder if you can give some help? Many thanks!

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

MATLAB >> How to close Excel workbook from Matlab

by Bin » Sat, 14 Nov 2009 01:33:18 GMT

I tried it but got the following error and wonder if you can give some help? Many thanks!

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

Similar Threads

1. Closing an excel workbook through Matlab - MATLAB

2. Adding Excel macro directly to Excel workbook

Hello!
I am generating a Excel Macro (VBA script file) from MATLAB How can I
directly add this Macro to specific workbook using MATLAB?
with regards,
YP
-------

3. How to close an excel file which opened already using any Matlab command - MATLAB

4. close excel file using matlab

Dear Sir
Shall i ask how to close excel file using matlab.
i used (winopen) to open the excel file (i must open and close it using MATLB relating to my application) .
                                         Thanks in advance 

5. Close Excel using MATLAB - MATLAB

6. Closing EXCEL only closes workbook - EXCEL '07

I seem to have an opposite problem than many others here.  I have a workbook 
open, and I want to close EXCEL '07 totally.  When I click the "x" in the 
right hand corner for EXCEL, it just closes the workbook I had open, and then 
I have to click the "X" again for close Excel.

Any hints ?  Thank you.
GJB

7. Closing Workbook with "X" closes all workbooks... - Excel

8. Closing a workbook from a macro doesn't close the workbook

This is in Excel 2003.

From one workbook I want to open another workbook, copy some data out and 
then close the other workbook.

The code is like this -

set wkb = workbooks("C:\BlahBlah\AnotherWorkbook,xls").open

<do some things that all work fine>

wkb.close

<go off and do something different>

When run in the VBA code window this works wonderfully well. Exactly as one 
would expect.

When this sub is assigned to a ctrl key and run from the worksheet pane all 
works  as expected with the exception that the external workbook is left open.
I'm obviously missing something but I haven't a clue what.
Can anyone oblige?

Cheers,

Dave