file.getFileAsync throwing 5001(internal error) when slice count is more than one Microsoft office word add-in

Viewed 224

In our company we have created the office word add-in and we are using Office.context.document.getFileAsync to get the file and file.getSliceAsync to get slices.

Recently we are facing an issue when slice count for a given file is more than one, we are getting internal error 5001.

public async getFile(fileType: Office.FileType): Promise<File> {
    return new Promise((resolve, reject) => {
        Office.context.document.getFileAsync(fileType,
            { sliceSize: 100000 },
            (result) => {
                if (result.status == Office.AsyncResultStatus.Succeeded) {
                    this.getAllSlices(result.value).then(
                        (val) => {
                            Office.context.document.getFilePropertiesAsync((properties) => {
                                ///converting bytes to blob object
                                    resolve(file);
                                } else {
                                    reject(properties.error);
                                    return;
                                }
                            });
                            result.value.closeAsync();
                        },
                        () => {
                            result.value.closeAsync();
                            reject('Error reading document');
                            return;
                        });
                } else {
                    result.value.closeAsync();
                    reject(result.error);
                    return;
                }
                result.value.closeAsync();
            });
    });
}

getSlices method

private getAllSlices(file: any): Promise<any> {
    let isError = false;

    return new Promise(async (resolve, reject) => {
        let fileData = [];
        for (let index = 0; (index < file.sliceCount) && !isError; index++) {
            let sliceReadPromise = new Promise((sliceResolve, sliceReject) => {
                file.getSliceAsync(index, (asyncResult) => {
                    if (asyncResult.status == Office.AsyncResultStatus.Succeeded) {
                        fileData = fileData.concat(asyncResult.value.data);
                        sliceResolve({
                            IsSucess: true,
                            Data: fileData
                        });
                    } else {
                        file.closeAsync();
                        sliceReject({
                            IsSucess: false,
                            ErrorMessage: `Error occured`
                        });
                    }
                });
            });
            await sliceReadPromise.catch(() => {
                isError = true;
            });
        }

        if (isError || !fileData.length) {
            reject('Error reading document');
            return;
        }

        file.closeAsync();
        resolve({
            IsSucess: true,
            Data: fileData
        });
    });
}

Script Lab project - https://gist.github.com/dula85/2d81a0dc4a82e6e4f88cabb916c8505a

In our workflow we are calling getFile method twice with Compressed and Pdf file type. internal error when slice count is 1


Fixed Figure out that the is issue with my code, I have file close statement in the getFile Method at the end which is causing this issue.

0 Answers
Related