views:

1352

answers:

2

Hello All!

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

using System; using System.Collections.Generic; using System.Linq; using System.Text; 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 dialogue 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  A: 

Hi yeahumok,

Thanks for the thanks. I'm glad it worked for you. :-)

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

Mike

Mike Rosenblum
again, THANK YOU for being a lifesaver!! your help is VERY appreciated
Sure, no problem! :-)
Mike Rosenblum
A: 

Hello Mike,

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];
        ws.PrintPreview(Type.Missing);

The compilaion is successful. but i didn't get the PrintPreview window. I dont konw how to achieve it . Please Guide me From, M.Thillai

M.Thillai