EsPl
EsPl

Reputation: 182

Excel Addin - How to trigger document download in desktop version

I am developing an Excel add-in using Office JS in which a user can decide to open a document from a remote server API. In that case I need to receive a file from the server API and open it in Excel.

I already found this answer: Programatically open a excel document using javascript office api

So I see that the API does not allow this, and the answer suggests triggering the download using JS.

So I added an iframe to the addin page:

<iframe id="download" width="1" height="1" style="display:none"></iframe>

And to trigger the document download I am doing:

document.getElementById("download").src = downloadURL;

That works perfectly when trying online (Office 365).

But it is not downloading the document using the desktop version.

How can I make the solution word in desktop Excel application?

Upvotes: 2

Views: 1555

Answers (2)

Michael Zlatkovsky
Michael Zlatkovsky

Reputation: 8670

Currently, there is no API in Excel to allow you to create/open document programmatically. Word does have it, and there is a suggestion -- currently under investigation-- for doing the same for Excel as well (https://officespdev.uservoice.com/forums/224641-feature-requests-and-feedback/suggestions/15034512-ability-to-open-replace-workbook-with-excel-js-api). But, there is not ETA that we can share yet.

That being said, you can absolutely pop up a "save as" dialog. In fact, if you've seen Script Lab, our "Export for publishing" does just that.

"Export for publishing" menu option

For Script Lab, we use the FileSaver NPM package. If you want to see the bit of Script Lab code that makes use of it, see this file, searching the area around FileSaver.saveAs. Note, however, that the save-as dialog does not seem to work correctly on Mac or iOS, so at the moment we limited this functionality to just Desktop and Office Online (you can see a platform check towards the beginning of that method).

Hope this helps!

Upvotes: 1

ashleedawg
ashleedawg

Reputation: 21639

If I'm understanding what you're trying to do, I have a potential solution.

One way to download a file from a URL with minimal coding is via the command-line with the Background Intelligent Transfer Service (BITSAdmin) Tool included with Windows.

This example downloads a cooking chart, in the background, to the location (and filename) you specify:

Sub testDownloadFileFromCMD()
    Const source_downloadURL = "http://instantpot.com/wp-content/uploads/2017/08/Cooking-Time-Tables-1.pdf"
    Const dest_PathFilename = "C:\downloadedPDFFile.pdf"
    Shell ("bitsadmin /transfer wcb /priority high " & source_downloadURL & " " & dest_PathFilename)
End Sub

It shows the progress window but that can be optionally hidden.

Does your URL work with BITS?

If this doesn't work then there are many other ways to download a file but I'll definitely need more information. I'm not sure what you mean by "the API allows Outlook365 but doesn't allow Excel to download the file". Is the API giving an error? More about this file... Is it Text/Binary? What kind of file. Is it large enough that size could be a problem? Are you able to download the file in other ways, like directly with a browser?

I hope this helps and if not I'll have more to say! :-)

Upvotes: 0

Related Questions