#INCLUDE c:\common\include\library.h #INCLUDE c:\common\include\vfp_excel.h * open Excel sheet LOCAL loExcel as Excel.Application LOCAL loWorkBook as Excel.Workbook LOCAL loActiveSheet as Excel.Sheets LOCAL loRange as EXCEL.Range LOCAL loPivot as Excel.PivotTable LOCAL lcFileName lcFileName = "MyTestFile.XLS" loExcel = CREATEOBJECT("Excel.Application") IF VARTYPE(loExcel) != "O" MESSAGEBOX("Could not instantiate Excel!", ; MB_ICONSTOP, "Error") RETURN .f. endif loExcel.Application.WindowState = xlMaximized && -4137 loWorkBook = loExcel.Workbooks.Open(lcFileName) * rename the sheet to work with loActiveSheet = loExcel.ActiveSheet loActiveSheet.Name = "Data" * create pivot table in existing workbook, then * delete original detail data sheet and save the pivot only with loExcel .Range("A1").Select .ActiveCell.FormulaR1C1 = "Tran Date" .Range("B1").Select .ActiveCell.FormulaR1C1 = "Country" .Range("C1").Select .ActiveCell.FormulaR1C1 = "Code" .Range("D1").Select .ActiveCell.FormulaR1C1 = "Trans" .Range("A1").Select endwith * find last used cell lcLastCell = ALLTRIM(STR(lnLstRow)) loActiveSheet.Range("A1:D" + lcLastCell).Columns.AutoFit() * make it of the form: "R999C4" (we always have 4 columns here) lcRCLastCell = "R" + lcLastCell + "C4" loPivot = loExcel.ActiveSheet.PivotTableWizard(xlDatabase, ; "Data!R1C1:" + lcRCLastCell, "", "Transactions", .T., .T.) loPivot.SaveData = .F. loPivot.AddFields("Tran Date", "Country", "Code") loPivot.PivotFields("Trans").Orientation = xlDataField * rename the sheet to yesterday's date * we use yesterday as today's data has not been received yet * (change "/" to "-" as slash is not legal in sheet names) LOCAL lcDataSheetName loActiveSheet = loWorkBook.Sheets("Sheet1") lcDataSheetName = strtran(dtoc(date()-1),"/","-") loActiveSheet.Name = lcDataSheetName * Start formatting * set Excel Print Area lcLastCell = loExcel.ActiveCell.SpecialCells(xlLastCell).Address() loActiveSheet.PageSetup.PrintArea = "$A$1:" + lcLastCell lcBetween = " between " + DTOC(ldStart) + " and " + DTOC(ldEnd) lcMsg = "Weekly transaction stats " + lcBetween * go to bottom and skip two rows for a message loExcel.Range(lcLastCell).Select loRange = loExcel.ActiveCell * extract column number from letter and subtract 65: CHR("A") n = ASC(SUBSTR(lcLastCell,2,1)) - 65 * move 2 down, and left to first column loRange = loRange.Offset(2,-n) loExcel.Range("A" + ALLTRIM(STR(loRange.Row))).Value = lcMsg * define printed page header/footer With loActiveSheet.PageSetup *.LeftHeader = "" .CenterHeader = "Transactions " + lcBetween *.RightHeader = "" .LeftFooter = "&BConfidential&B" .CenterFooter = "&D" .RightFooter = "Page &P" .PrintGridlines = .F. .CenterHorizontally = .T. .CenterVertically = .F. *.Orientation = xlPortrait endwith * SaveAs in latest XLS format lcFileName = "T" + DTOS(DATE()-1) + ".XLS" lnFileFormat = xlNormal lcPassword = "" lcWriteResPassword = "" llReadOnlyRecommended = .F. llCreateBackup = .F. loWorkbook.SaveAs(lcFileName, lnFileFormat, lcPassword, ; lcWriteResPassword, llReadOnlyRecommended, llCreateBackup)* etc. etc.