4

I have the following code: [Thank you Mike Rosenblum!]

using System;
using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;

namespace ConsoleApplication17
{
    class Program
    {
        static void Main(string[] args)
        {

            //public void PrintMyExcelFile() 
            //{
            Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();

            // Open the Workbook:
            Microsoft.Office.Interop.Excel.Workbook wb = excelApp.Workbooks.Open(
                @"C:\hello.xls",
                Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing);

            // Get the first worksheet.
            // (Excel uses base 1 indexing, not base 0.)
            Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[1];

            // Print out 1 copy to the default printer:
            ws.PrintOut(
                Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing);

            // Cleanup:
            GC.Collect();
            GC.WaitForPendingFinalizers();

            Marshal.FinalReleaseComObject(ws);

            wb.Close(false, Type.Missing, Type.Missing);
            Marshal.FinalReleaseComObject(wb);

            excelApp.Quit();
            Marshal.FinalReleaseComObject(excelApp);
        }
    }
}

What I'm trying to accomplish is that instead of this printing my Excel file right away, I'd like a print dialog to appear so that I may choose a specific printer if I'd like. I'm using the 12.0.0.0 .NET interop for Excel. Anybody have any ideas?

Thanks in advance.

1 Answer 1

8

The print dialog is accessible from .NET and will run just fine on Excel 2007 using the 12.0 PIAs. The Dialog.Show() command, however, has 30 optional parameters. In the future, C# 4.0 will allow for omitting optional parameters (thank goodness), and VB.NET does not require them, but if using C# 3.0 or below, we have to provide Type.Missing for the optional parameters. All 30 of them:

bool userDidntCancel =
    excelApp.Dialogs[Excel.XlBuiltInDialog.xlDialogPrint].Show(
        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

The the Show() method returns 'true' to indicate that the operation succeeded; it returns 'false' to indicate that the user hit the cancel button or the escape (esc) key, so no action occurred.

Hope this gets you going...

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

4 Comments

again, THANK YOU for being a lifesaver!! your help is VERY appreciated
I had gone through the codings above. I too have a problem in a similar process i.e Print Preview option. Excel.Application excelApp = new Excel.Application(); Excel.Workbook wb = excelApp.Workbooks.Open(@"C:\\Documents and Settings \\Admin \\Desktop \\DoCoMo\\ news5.xls",Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];
After trying the solution, the printer dialog doesn't pop up. My website just hangs forever. Do you the possible way to rectify it? Thanks!
@john - website hangs? I wouldn't try this from server side code. This means the print dialog is probably showing up on the server and yes, it will hang.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.