23

I am using Excel Interop to open an xlsx file and save that as a pdf document. Upon invoking the 'ExportAsFixedFileFormat' method a dialog titled "Publishing" is displayed to indicate the progress. How can I suppress or hide this dialog? I have seen a few similar questions on other forums without a satisfying solution, but hopefully someone has solved this since then.

enter image description here

Code:

Application application = new Application();
application.DisplayAlerts = false; // <- No effect
application.Visible = false; // <- No effect
application.ScreenUpdating = false; // <- No effect
application.UserControl = false; // <- No effect
application.Workbooks.Open(path, Type.Missing, true);
application.DisplayDocumentActionTaskPane = false; // <- No effect
application.Worksheets[1].ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, path);
bjornno
  • 251
  • 3
  • 4
  • This Bounty might help noone. It seems to depend on OS and Excel-Version, since on my Machine (Win 10 + Office 2013) not a single dialog is popping – lokusking Aug 09 '16 at 20:10
  • You simply can't. These sort of progress dialogs appear when Excel senses that the operation in progress is going to take long time. Even if you save your workbook `CTRL+S` and the destination is a network drive with slow network you will see a progress dialog. Its mostly because you either you are publishing alot of data or your system is resource strapped or you are publishing on a network drive which is slow or all of these. – cyboashu Aug 09 '16 at 22:29
  • 3
    Few overkill ways to do this are, using WinAPI to find this dialog and set visibility to hidden or if you are concerned with user cancelling this operation, then again use WinAPI to block keyboard mouse input during the publication. – cyboashu Aug 09 '16 at 22:31
  • When I run your code (excel 2016) it says "no printer is installed, etc.". It looks like 1) it depends heavily on Excel version, 2) it depends heavily on what's installed, and 3) it depends on 3rd parties. Maybe the window is a 3rd party one (printer of some sort), not stricly speaking an Excel one. Why can't you use SaveAs (+pdf) instead of Export? – Simon Mourier Aug 11 '16 at 06:19
  • Have you tried this? http://stackoverflow.com/a/29101847 – Mr. C Aug 11 '16 at 19:07
  • Hi Michael, can you see this comment in your alerts? As per http://meta.stackoverflow.com/questions/332259/tag-the-altruist-in-comments-on-a-bounty-question – Jeremy Thompson Aug 12 '16 at 09:47
  • Sorry to bother you again, but does this get your attention @MichaelBrandonMorris ? *Good luck with that Honors College class of 2018.* – Jeremy Thompson Aug 12 '16 at 11:21
  • 1
    Yes, I was notified. Thanks. – Michael Brandon Morris Aug 12 '16 at 13:39
  • From both comments Michael? or just the last one with the @ - To answer your bounty, it is a case of WinAPI32 FindWindow and once you get the handle to the window hiding it. I would urge you against this method and just leave the default "Publishing" window. If this is embedded in your app, simply centre the window via a WinAPI32 call insteadl – Jeremy Thompson Aug 12 '16 at 13:45
  • This may not be what you want to hear, it wasn't for me either. I have run into several issues with either briefly displaying dialogs or just impossible to keep closed ones. – Nathan Bellowe Aug 13 '16 at 08:51
  • Have you tried this http://stackoverflow.com/questions/25803707/how-to-hide-publishing-progress-bar-for-exportasfixedformat – Huu Thien Tan Nguyen Sep 23 '16 at 14:58
  • @SimonMourier `Why can't you use SaveAs (+pdf) instead of Export? ` Because `SaveAs` with a pdf extension creates an excel with pdf extension which is not valid. – GuidoG Feb 22 '21 at 06:59
  • Did you ever found a solution for this ? – GuidoG Feb 22 '21 at 07:00

4 Answers4

1

Perhaps I am not answering your question, but I'll try to be helpful. As far as I know, it's not recommended to use Excel Interop in back-end processing. You encountered one of its issues, but there are some others. There are 3rd party alternatives for interacting with Excel formats, some of them are open source and some are commercial. I would strongly recommend taking a look at one of them: ClosedXML and NPOI. Both are open source and free.

Boris Modylevsky
  • 3,029
  • 1
  • 26
  • 42
0

How about processing it on a BackgroundWorker. This will not throw COMEXCEPTION. I tried this code on a button click and it works without displaying the progress dialog.

var worker = new BackgroundWorker();
worker.DoWork += (o, args) =>
{
    var path = @"D:\sample.xlsx";
    var application = new Microsoft.Office.Interop.Excel.Application();
    application.Workbooks.Open(path,
        Type.Missing, true);
    application.Worksheets[1].ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, path);
};
worker.RunWorkerAsync();
jmvtrinidad
  • 3,347
  • 3
  • 22
  • 42
0

You can use pedamorf library ,pedamorf is a server that converts documents, images, urls, html and text to PDF. It relies heavily upon the excellent open-source programs wkhtml2pdf, Libre Office and iTextSharp. A client library is provided and can be used from a .NET application to utilize the conversion services provided by pedamorf.

More info is here

0

By far the better API to handle (create, read, export to PDF/html, etc...) XLS/XLSX files that I know is FlexCel. It's really simple, incredibly fast and full writen in C#. Don't require Excel instalation.

You'll find it here : http://www.tmssoftware.com/site/flexcelnet.asp (get the trial version with a lot of examples with source code)

If you have budget in your project to acquire the licence, I realy reccomend.

  • Use NuGet and search for EPPlus. It's by far (in my HO) the BEST API for generating with Excel content on the web. –  Apr 04 '17 at 16:38