Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi..

As i want Print the Particular Excel Sheet from excel file.i wrote some coding to print Excel sheet but i got some errors while print the document.

Error Details

exception from hresult 0x800a03ec

My Code:

C#
Excel.Application excelApp = new Excel.Application();
            // Open the Workbook:
           // Excel.Workbook wb = excelApp.Workbooks.Open(@"C:\My Documents\Book1.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);
            string dtime;
            dtime = day + "-" + month + "-" + year;
            Excel.Workbook wb = excelApp.Workbooks.Open("D:\\Saved Sheets\\" + lblusername + "\\" + file_name + "-" + dtime + ".xlsx", 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.)
            Excel.Worksheet ws = (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);
            ws.PrintOutEx(Type.Missing, 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);

How can i solve this Problem Please sent solution ASAP.
Thank You.
Posted
Updated 4-Jul-13 3:01am
v2

1 solution

Before you start printing, you need to set PageSetup[^]:
1) PrintArea[^]
2) FitToPageTall[^] or FitToPageWide[^] or Zoom[^]
3) Orientation[^]
4) PaperSize[^]
5) etc.

More:
Worksheet.Printout[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900