Board index » delphi » How to open and print xls-file.
Ren?Laursen
![]() Delphi Developer |
How to open and print xls-file.2003-10-29 09:40:04 PM delphi200 How do I open an xls-file, display a printer-dialog and print to the chosen printer ? I am using Delphi 7 and the 2000-server-components, have tried code as shown below, but it is failing when trying to set activeprinter and when calling PrintOut. What is wrong ? Ren?/ SSV ... ExcelApplication1: TExcelApplication; WBk: TExcelWorkbook; WS: TExcelWorksheet; ... LCID: integer; Prt: OleVariant; ... ExcelApplication1.Connect; LCID:= GetUserDefaultLCID; try if PrintDialog1.Execute then begin Prt:= Printer.Printers[Printer.PrinterIndex]; try Excelapplication1.ActivePrinter[lcid]:= Prt; except end; // Fails !? Wbk.ConnectTo(ExcelApplication1.Workbooks.Open(<Somefile>,EmptyParam, EmptyParam,EmptyParam,EmptyParam, EmptyParam,EmptyParam,EmptyParam, EmptyParam,EmptyParam,EmptyParam, EmptyParam,EmptyParam,LCID)); WS.ConnectTo(Wbk.worksheets[1] as _Worksheet); WS.Activate; WS.PageSetup.Orientation:= xlLandscape; WS.PageSetup.Papersize:= xlPaperA4; try WS.PrintOut; // Fails !!? except ExcelApplication1.ActiveWindow.SelectedSheets.PrintOut(EmptyParam, EmptyParam, 1, EmptyParam, Prt, EmptyParam, true, EmptyParam, LCID); // Fails !! ?? end; end; finally ExcelApplication1.DisplayAlerts[lcid]:= false; WS.Disconnect; Wbk.Close; Wbk.Disconnect; ExcelApplication1.Quit; ExcelApplication1.DisConnect; end; |