3

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";
xlApp.Dialogs[Excel.XlBuiltInDialog.xlDialogPrint].Show();
3
  • The mentioned .Show() method is not for the Excel application itself, but for individual dialog boxes within Excel it seems. Commented Mar 24, 2011 at 17:12
  • @froeschli That is correct, but from what I understand, the arguments passed to Dialogs[].Show() are passed on to the Print Dialog, which is what is causing the undesired behavior. Commented Mar 24, 2011 at 17:57
  • can you show us any code, which you are actually using to show the print dialog box? Commented Mar 24, 2011 at 18:06

2 Answers 2

1

You might try adding

xlApp.Visible = False

after:

xlApp.Dialogs[Excel.XlBuiltInDialog.xlDialogPrint].Show(); 

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I've tried calling xlApp.Visible = False before the .Show() earlier but that didn't do anything. I didn't consider calling it after. Your suggestion "sort of" works. It quickly displays and hides the Excel window to the user. I could potentially live with that, but it isn't preferred.
I chose this since this seems to be the closest I am going to get to my desired behavior. Thanks!
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.