Search code examples
c#interopprinting

Using Excel Interop and getting a print dialog


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.


Solution

  • 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...