MATLAB Answers

Actxserver unable to locate Excel workbook

5 ビュー (過去 30 日間)
Bob Nbob
Bob Nbob 2016 年 7 月 28 日
コメント済み: Bob Nbob 2016 年 7 月 28 日
I am having issues getting the ActiveX application to open an existing Excel doc for editing.
My current code looks like:
file_name = 'Excel_Book.xlsx';
xl = actxserver('Excel.Application');
xlb = xl.Workbooks.Open(file_name);
This returns the error:
Error
Invoke Error, Dispatch Exception:
Source Microsoft Excel
Description: 'Excel_Book.xlsx' could not be found
I know that the file exists, as I can read it with
xlsread(file_name)
I have even changed the MATLAB directory to be seeing the folder with the Excel_Book file in it, and yet the ActiveX refuses to read the document.
Similar problems were encountered using:
xlb = xl.Workbooks.Item(file_name);
or
xlb = xl.get('Workbooks',file_name);

  0 件のコメント

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2016 年 7 月 28 日
It would be safer to use fullfile() to create a fully-qualified file name to request to open.

  3 件のコメント

Bob Nbob
Bob Nbob 2016 年 7 月 28 日
Ok. Do you know if there is a quick and easy way of getting both the file name and the directory from user input. Something like:
filename = uigetfile
filedir = uigetdir
But all in one command? Or possibly a command to automatically get the directory from the user chosen file?
Walter Roberson
Walter Roberson 2016 年 7 月 28 日
[filename, filedir] = uigetfile();
complete_file_name = fullfile(filedir, filename);
Bob Nbob
Bob Nbob 2016 年 7 月 28 日
[filename, filedir] = uigetfile();
That second field is exactly what I needed, thanks.

サインインしてコメントする。

その他の回答 (1 件)

Shameer Parmar
Shameer Parmar 2016 年 7 月 28 日
Try replacing the line and check..
xlb = xl.Workbooks.Open(file_name);
with
xlb = xl.Workbooks.Open('fullpath\file_name along with the extension');

  0 件のコメント

サインインしてコメントする。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by