Closed ig-jb closed 6 months ago
@ig-jb Currently, we don't have a direct way to get the file name. As an workaround, Could you try getFilePropertiesAsync like below to see if it can meet your requirement ? For Office.context.document.url didn't get updated, i log a BUG 4655764 for internal track
Office.context.document.getFilePropertiesAsync(function (asyncResult) { var fileUrl = asyncResult.value.url; console.log(fileUrl); });
Besides, You can add a request on our UserVoice page if you think providing a direct API to get document name is more helpful.
Being able to get the document's name is very useful for scenarios where we want to create an add-in uploading specific file to third-party systems.
Mandy's solution is correct, code snippet for converting the callback to a promise for use in async:
const promise = new Promise((resolve, reject) => { Office.context.document.getFilePropertiesAsync(function (asyncResult) { const fileUrl = asyncResult.value.url; if (!fileUrl || fileUrl === "") { reject('no url') } else { resolve(fileUrl); } }) })
Note also that this api can return file paths so you need to check for that.
Currently, I believe the only way to get access to the document name is to parse Office.context.document.url. This value gets updated after document reload (i.e. close and reopen) if the document is renamed; however the old value remains accessible after rename unless the user performs the reload.
Expected Behavior
Office.context.document.url references the latest value for document url after renaming the document
Current Behavior
The old name of the document remains present in Office.context.document.url
Steps to Reproduce, or Live Example
Context
We are replacing a feature from a VSTO addin which allowed the user to save Excel or Word documents to our web application. This limitation means the old name gets used when uploading the document; which will lead to user confusion.
Your Environment