xlswrite1, Object returned error code: 0x800A03EC at line 142

1 view (last 30 days)
I am first time using xlswrite1 and tried to not use xlswrite in the for loop, I want to enter the data from a structure, which contains 27 fields, each field has Mx1 Data,
I first tried with xlswrite:
for VariableNum = 1:length(fieldnames(Data))
xlswrite(FileAddress,Data.(ColumnName{VariableNum}),1,[Column{VariableNum} num2str(4)]);
end
Although it took me half minute to complete. the above line worked perfectly fine. and then i tried with the xlswrite1, didn't change any input except adding a few lines to open excel,
Excel = actxserver ('Excel.Application');
File=FileAddress;
if ~exist(File,'file')
ExcelWorkbook = Excel.workbooks.Add;
ExcelWorkbook.SaveAs(File,1);
ExcelWorkbook.Close(false);
end
for VariableNum = 1:length(fieldnames(Data))
xlswrite1(FileAddress,Data.(ColumnName{VariableNum}),1,[Column{VariableNum} num2str(4)]);
end
invoke(Excel.ActiveWorkbook,'Save');
Excel.Quit
Excel.delete
clear Excel
I uses this xlswrite1 in my custom function, which is used in a GUI function. I also changed the second line in xlswrite1 from
Excel=evalin('base','Excel');
to
Excel=evalin('caller','Excel');
as suggested.
for some reason this gave me error indicated in the title.

Accepted Answer

Image Analyst
Image Analyst on 30 Apr 2014
Why did you do this to close your workbook:
ExcelWorkbook.Close(false);
I would think that xlswrite1() would expect that Excel has the workbook open. If you don't have an active workbook open, where will it write to? You just have Excel open with no workbook, and no worksheet in the workbook (because the workbook itself is not there). Try getting rid of the .Close call and see if that works.
  1 Comment
LinKun
LinKun on 30 Apr 2014
It works like a charm! thank you so much! I looked at the description of the xlswrite1 in the file exchange and saw that i need to add those line, so i didn't go in deep to understand those lines. So the .Close line is included because it was there in the description.

Sign in to comment.

More Answers (1)

Austin Bond
Austin Bond on 8 Oct 2018
Edited: Austin Bond on 8 Oct 2018
So I recently encountered the same error while using the xlswrite1.m function
Error using COM.Excel_Application/Range Error: Object returned error code: 0x800A03EC
It turns out there is a bug in the xlswrite1.m function that has to due with converting the column number into a column letter. For example, I was exporting data that had 684 columns, which would correspond to a column LETTER 'ZH'.
The xlswrite1.m function I have uses the functions:
s = dec2base27(d) d = base27dec(s)
to convert column number to column letter. The problem is this algorithm fails for column letters "ZA -- ZZ".
The solution is pretty simple (NOTE: SEE ATTACHED .M FILE):
function range = calcrange(range,m,n) ... ... ... LINE 260: % lastcol = dec2base27(base27dec(firstcol)+n-1); % DELETE THIS LINE OF CODE!!! THIS IS WHERE THE ERROR IS
% THIS IS THE SOLUTION TO THE PROBLEM % - Basically just use MathWorks canned functions from the xlswrite.m function, specifically calcrange.m % columnNumber.m - xlswrite1.m will need access to these (whether in the main body of the script, same directory, or added to the path) % columnLetter.m - xlswrite1.m will need access to these (whether in the main body of the script, same directory, or added to the path)
%ANSWER IS HERE!!! RIGHT BELOW!!!
lastcol = columnLetter(columnNumber(firstcol)+n-1); % Construct last column. ADD THIS LINE OF CODE

Community Treasure Hunt

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

Start Hunting!