Why does accessing an Excel Sheet throw the error "Invoke Error, Dispatch Exception: Invalid index"?
10 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
MathWorks Support Team
il 3 Lug 2019
Risposto: MathWorks Support Team
il 3 Lug 2019
When I try to access and Excel Sheet I get the following error:
K>> outputFile.Sheets.Item(3)
Error using Interface.000208D7_0000_0000_C000_000000000046/Item
Invoke Error, Dispatch Exception: Invalid index.
I am using the piece of code below, which used to work before:
try
app = actxserver('Excel.Application');
catch
error('Could not establish a link to Excel');
end
workbooks = app.Workbooks;
outputFile = workbooks.Add;
outputFile.Sheets.Add;
resultsSheet = outputFile.Sheets.Item(1);
exclusionSheet = outputFile.Sheets.Item(2);
summarySheet = outputFile.Sheets.Item(3);
pfoWgtNanSheet = outputFile.Sheets.Item(4);
Risposta accettata
MathWorks Support Team
il 4 Lug 2019
This issue occurs when trying to access a non-existent Excel sheet. In this case, "outputFile.Sheets.Item(3)" is trying to access the third sheet, however, it does not exist in the Workbook.
The original Excel workbook contains only one sheet, another one is added later on, so there are only two by the time the third one is accessed. Different versions of Excel may create three sheets by default and that is the reason why this script may have worked before. This behavior is generally only dependent on the Excel version and not the MATLAB Release. To avoid this, please always ensure that the workbook contains the intended number of sheets.
This issue can occur when it creates a link and then deletes the corresponding cell from Excel.
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Data Import from MATLAB in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!