Home » excel » c# – Excel print dialog opens unwanted instance of Excel after clicking OK

c# – Excel print dialog opens unwanted instance of Excel after clicking OK

Posted by: admin April 23, 2020 Leave a comment


I am using xlApp.Dialogs[Excel.XlBuiltInDialog.xlDialogPrint].Show() to show a print dialog to the user to print the contents of an Excel Workbook which was created programmatically.

The printing part of it works just fine after the user clicks OK. However, in addition to printing, the program also opens and displays an instance of Excel. This isn’t a print preview, but the actual Excel document being opened and displayed to the user.

My goal is to show the user the print dialog, allowing them to print without showing Excel.

I have looked here in an effort to find some sort of method or argument to disable opening Excel. Unfortunately, all I’ve discovered is I can supply .Show() with 30 arguments, but not which argument corresponds to what setting.

I haven’t been able to find anything on SO and my google skills seem to be failing me today.

Update: ‘Excel’ in my code is an alias: using Excel = Microsoft.Office.Interop.Excel;

I was able to find the Built-In Dialog Box Argument Lists. Looking at “xlDialogPrint” I am guessing these are the arguments that go into .Show(). However, nothing in this list seems to prevent Excel from opening.

Another Update: Simplest code to reproduce the behavior

Excel.Application xlApp;
Excel.Workbook xlWB;
Excel.Worksheet xlWS;
xlApp = new Excel.Application();
xlWB = xlApp.Application.Workbooks.Add();
xlWS = xlWB.Sheets[1];
xlWS.Cells[1][1] = "TEST";
How to&Answers:

You might try adding

xlApp.Visible = False



I haven’t tested it but it might work.

However you will still have an instance of Excel running in the background – to close it down you will need to call xlApp.Quit, and also to be aware of the issue described in this KB article.


It’s ugly, but you can add the following lines before showing the dialog

var last_height = xlApp.Height,
    last_width = xlApp.Width;
xlApp.Height = 0;
xlApp.Width = 0;

and the following before you close Excel

xlApp.Visible = false;
xlApp.Height = last_height;
xlApp.Width = last_width;

I wrote in JScript but it’s easy to translate.