1

I am building an Excel Add-in with the Office JS API. Is there a way to handle printing with the Add-in or open a printing window?

I have been going through the documentation but can't find any methods for this. Am I missing something?

Edit: To be more specific, I want to open the Excel print dialog with the Add-in.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • @RickKirkham that doesn't seem 100% accurate to me. If a user designed a print button in the "Web App" it would likely print the "Web Page". Is there anything in the Excel API for printing so a button in the "WebApp" can communicate with the Excel API and print the `spreadsheet`? – FreeSoftwareServers Dec 12 '22 at 20:41
  • @MarkoCubic I don't think you can actually get a document to print, you can setup print properties like `fit columns on one page`, but there has always been a disconnect between apps and printing. For example, you can't set `duplex`. In VBA I'd probably resort to `sendkeys` or using `batch` but that isn't possible with JS due to Browser Security. Maybe you can get the print dialog to show? – FreeSoftwareServers Dec 12 '22 at 20:48
  • https://learn.microsoft.com/en-us/office/dev/add-ins/excel/excel-add-ins-worksheets#page-layout-and-print-settings – FreeSoftwareServers Dec 12 '22 at 20:48
  • @RickKirkham I forgot to specify that I want to open the Excel print dialog using the Add-in, just like FreeSoftwareServers pointed out. – Marko Cubic Dec 13 '22 at 11:53
  • 1
    @FreeSoftwareServers The print dialog showing is all I need actually but not sure how to achieve that. – Marko Cubic Dec 13 '22 at 11:56
  • @MarkoCubic unless somebody points out a way w/ Excel API, I'd suggest looking into bridging VBA and Office-JS perhaps. Its been on my "todo list" as there are things that JS just can't do that VBA can (for security reasons) – FreeSoftwareServers Dec 13 '22 at 18:19
  • @MarkoCubic Thanks. Your edit of your question changes its meaning. I will delete my original comment. – Rick Kirkham Dec 14 '22 at 07:17

1 Answers1

1

There is no API in Office.js to invoke the Excel print dialog. If you know that all the users of your add-in will be working on Windows machines, using desktop Excel, you might look into whether a VSTO/VBA add-in has this feature. But if some users will be using Excel on the web or Excel on Mac, you'll have to stick to an Office.js add-in. In the later case, the best you can do is have your add-in prompt and/or instruct users to print from the Excel UI. You can have some control over the layout of the worksheet when it prints. See Work with worksheets - Page layout and print settings for more information.

You can suggest that Office add-ins be given access to the print UI at Microsoft 365 Developer Platform Ideas, but I suspect that security concerns would make it unlikely that Office.js-based add-ins, which are web applications, would be given that feature. The JavaScript/HTML that is hosted by a web application can be changed by its creator after the add-in has been installed by a user and/or approved by an administrator. This makes it possible for a rogue add-in creator to change the behavior of the add-in post-installation.

Rick Kirkham
  • 9,038
  • 1
  • 14
  • 32